$a = 10;
$b = 3;
echo $a + $b; // 13
echo $a - $b; // 7
echo $a * $b; // 30
echo $a / $b; // 3.33
echo $a % $b; // 1Assignment operators are used to assign and update variable values:
= — Assigns a value: $x = 5;+= — Adds and assigns: $x += 3; is the same as $x = $x + 3;-= — Subtracts and assigns: $x -= 2; is the same as $x = $x - 2;*= — Multiplies and assigns: $x *= 4; is the same as $x = $x * 4;/= — Divides and assigns: $x /= 6; is the same as $x = $x / 6;%= — Modulus and assigns: $x %= 3; is the same as $x = $x % 3;$x = 5;
$x += 3; // 8
$x -= 2; // 6
$x *= 4; // 24
$x /= 6; // 4
$x %= 3; // 1Comparison operators are used to compare values and return a boolean result (true or false):
== — Equal (compares values, not types)=== — Identical (compares values and types)!= — Not equal (compares values, not types)!== — Not identical (compares values and types)> — Greater than< — Less than>= — Greater than or equal to<= — Less than or equal to$x = 5;
$y = "5";
var_dump($x == $y); // true (same value, type ignored)
var_dump($x === $y); // false (type mismatch: int vs string)
var_dump($x != $y); // false (same value)
var_dump($x !== $y); // true (different types)
var_dump($x > 3); // true
var_dump($x <= 5); // trueLogical operators are used to combine conditional statements:
&& (AND) – Returns true if both conditions are true.|| (OR) – Returns true if at least one condition is true.! (NOT) – Reverses the boolean value.$a = true;
$b = false;
var_dump($a && $b); // false (only one is true)
var_dump($a || $b); // true (at least one is true)
var_dump(!$a); // false (not true)PHP provides two main string operators:
. — Concatenation Operator: Combines two strings together.$greeting = "Hello" . " World";.= — Concatenation Assignment Operator: Appends the right-hand string to the left-hand variable.$greeting .= " World";$i = 1;
echo ++$i; // 2 (pre-increment)
echo $i++; // 2 (post-increment), then i = 3$x = array("a" => 1, "b" => 2);
$y = array("b" => 2, "a" => 1);
var_dump($x == $y); // true
var_dump($x === $y); // false (order matters)The spaceship operator <=> (also known as the combined comparison operator) is used to compare two values. It returns:
-1 if the left operand is less than the right0 if both operands are equal1 if the left operand is greater than the rightThis operator is especially useful for custom sorting logic.
It works with strings as well, compares strings by alphabetical order. It gives -1 if the first string comes before the second, 0 if they are the same, and 1 if it comes after.
??Returns the first value that is not null:
$name = $_GET['name'] ?? "Guest";
echo $name;Ask the AI if you need help understanding or want to dive deeper in any topic