PHP Operators
PHP's operators cover the usual arithmetic, comparison, logical, and assignment — plus a few PHP-specific niceties.
Arithmetic
+ - * / %, ** (power), and intdiv($a, $b) for integer division.
String
| Op | Means |
|---|---|
. | Concatenation. Yes, dot — not +. |
.= | Append in place. |
Comparison
| Op | Means |
|---|---|
== != | Loose — type juggling. |
=== !== | Strict — same type AND value. Prefer this. |
< > <= >= | Magnitude. |
<=> | Spaceship — returns -1, 0, or 1 for sorting. |
Logical
| Op | Means |
|---|---|
&& \|\| ! | High-precedence boolean ops. |
and or xor | Lower-precedence keywords — avoid mixing with assignment. |
Null-safe operators
PHP
$name = $user->profile?->name; // 8.0+ null-safe chain $email = $_POST['email'] ?? 'n/a'; // null coalesce $_POST['active'] ??= '1'; // assign only if not set echo $value ?: 'fallback'; // elvis — empty/falsy fallback
Assignment shortcuts
= += -= *= /= %= **= .= ??= all combine an operator with assignment.
Increment / decrement
PHP
$i = 5; echo $i++; // 5 — post-increment, prints then bumps echo $i; // 6 echo ++$i; // 7 — pre-increment, bumps then prints
Tip: Default to
=== / !==. Reach for loose == only when you genuinely want type juggling (rare).Example
Example
<?php echo 2 + 3, ' ', 'a' . 'b', PHP_EOL; var_dump(1 == '1', 1 === '1'); var_dump(null ?? 'fallback'); var_dump(0 ?: 'zero');Try it Yourself »
Exercise
Null-coalesce operator.
$name = $_POST['name']
'n/a';
Two question marks.
Discussion
Loading…