PHP Operators

Arithmetic Operators

$a = 10;
$b = 3;

echo $a + $b;  // 13
echo $a - $b;  // 7
echo $a * $b;  // 30
echo $a / $b;  // 3.33
echo $a % $b;  // 1

Assignment Operators

Assignment 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;   // 1

Comparison Operators

Comparison 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);    // true

Logical Operators

Logical 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)

String Operators

PHP provides two main string operators:

  • .Concatenation Operator: Combines two strings together.
    Example: $greeting = "Hello" . " World";
  • .=Concatenation Assignment Operator: Appends the right-hand string to the left-hand variable.
    Example: $greeting .= " World";
Loading...
Output:

Increment / Decrement

$i = 1;
echo ++$i;  // 2 (pre-increment)
echo $i++;  // 2 (post-increment), then i = 3

Array Operators

$x = array("a" => 1, "b" => 2);
$y = array("b" => 2, "a" => 1);

var_dump($x == $y);   // true
var_dump($x === $y);  // false (order matters)

Spaceship Operator

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 right
  • 0 if both operands are equal
  • 1 if the left operand is greater than the right

This operator is especially useful for custom sorting logic.

Loading...
Output:

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.

Loading...
Output:

Null Coalescing Operator ??

Returns the first value that is not null:

$name = $_GET['name'] ?? "Guest";
echo $name;

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic