Lesson 3 • Beginner
Operators & Expressions ➕
Master PHP's arithmetic, comparison, logical, and string operators — including the spaceship and null coalescing operators.
What You'll Learn in This Lesson
- • Arithmetic operators: +, -, *, /, %, ** (exponentiation)
- • The critical difference between == (loose) and === (strict)
- • PHP 7+ operators: spaceship <=> and null coalescing ??
- • Logical operators: &&, ||, ! for combining conditions
- • String concatenation with the . (dot) operator
1️⃣ Operator Categories
| Category | Operators | Example |
|---|---|---|
| Arithmetic | + - * / % ** | $a + $b |
| Assignment | = += -= *= /= .= | $x += 5 |
| Comparison | == === != !== < > <=> | $a === $b |
| Logical | && || ! and or | $a && $b |
| String | . .= | "Hi " . $name |
| Null Coalescing | ?? ??= | $x ?? "default" |
Try It: Arithmetic & String Operators
Practice arithmetic, assignment, increment/decrement, and string concatenation
// PHP Operators (simulated in JavaScript)
console.log("=== Arithmetic Operators ===");
console.log();
let a = 15;
let b = 4;
console.log("$a = " + a + ", $b = " + b);
console.log();
console.log("$a + $b = " + (a + b) + " (Addition)");
console.log("$a - $b = " + (a - b) + " (Subtraction)");
console.log("$a * $b = " + (a * b) + " (Multiplication)");
console.log("$a / $b = " + (a / b) + " (Division — returns float!)");
console.log("$a % $b = " + (a % b) + " (Modulus — remainder)");
co
...Try It: Comparison & Logical Operators
Learn the crucial difference between == and ===, plus PHP 7+ operators
// PHP Comparison & Logical Operators
console.log("=== Comparison Operators ===");
console.log();
// The crucial difference: == vs ===
console.log("IMPORTANT: == (loose) vs === (strict)");
console.log();
console.log(" 0 == false → " + (0 == false) + " (loose: type juggling!)");
console.log(' 0 === false → ' + (0 === false) + " (strict: different types)");
console.log();
console.log(' "5" == 5 → ' + ("5" == 5) + " (loose: string converted)");
console.log(' "5" === 5 →
...⚠️ Common Mistakes
0 == "hello" is true in PHP! Always prefer ===.. for concatenation. "5" + "3" equals 8 (numeric addition!).if ($x = 5) assigns 5 to $x (always true). Use == or === for comparison.?? (null coalescing) instead of isset() checks — it's cleaner and more readable.📋 Quick Reference — PHP Operators
| Operator | Meaning | Example |
|---|---|---|
| === | Strict equal | $a === $b |
| <=> | Spaceship (3-way) | $a <=> $b → -1/0/1 |
| ?? | Null coalescing | $x ?? "default" |
| . | String concat | "Hi " . $name |
| ** | Exponentiation | 2 ** 10 → 1024 |
🎉 Lesson Complete!
You've mastered PHP operators! Next, learn how to control program flow with if/else and switch statements.
Sign up for free to track which lessons you've completed and get learning reminders.