Arithmetic
+ - * / % **
Assignment
PHP
<?php
$x = 10;
$x += 5; // 15 (same as $x = $x + 5)
$x -= 3; // 12
$x *= 2; // 24
$x /= 4; // 6
$x %= 4; // 2
$s = "a";
$s .= "b"; // "ab" — string appendComparison
| Operator | Meaning |
|---|---|
== |
equal (after type juggling) |
=== |
identical (same value and type) |
!= / <> |
not equal |
!== |
not identical |
< > <= >= |
ordering |
<=> |
spaceship: returns -1, 0, or 1 |
PHP
<?php
var_dump(1 <=> 2); // -1
var_dump(2 <=> 2); // 0
var_dump(3 <=> 2); // 1The spaceship operator exists mainly to make sort callbacks short:
PHP
<?php
usort($books, fn($a, $b) => $a['price'] <=> $b['price']);Logical
PHP
<?php
$a = true; $b = false;
var_dump($a && $b); // false — and
var_dump($a || $b); // true — or
var_dump(!$a); // false — notand/or also exist but have lower precedence than =, which causes
subtle bugs. Always use && and ||.
Increment / decrement
PHP
<?php
$i = 5;
echo $i++; // prints 5, then $i becomes 6 (post-increment)
echo ++$i; // $i becomes 7, then prints 7 (pre-increment)Null-safe operators
PHP
<?php
// ?? — use the right side if the left is null or undefined
$name = $_GET['name'] ?? 'Guest';
// ??= — assign only if currently null
$config['limit'] ??= 20;
// ?-> — call a method only if the object isn't null (PHP 8+)
$city = $user?->address?->city;?? is the single most useful modern PHP operator — it replaces a pile
of isset() checks.
Ternary
PHP
<?php
$status = $age >= 18 ? "adult" : "minor";