iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

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

OpMeans
.Concatenation. Yes, dot — not +.
.=Append in place.

Comparison

OpMeans
== !=Loose — type juggling.
=== !==Strict — same type AND value. Prefer this.
< > <= >=Magnitude.
<=>Spaceship — returns -1, 0, or 1 for sorting.

Logical

OpMeans
&& \|\| !High-precedence boolean ops.
and or xorLower-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';

Test yourself

Q1. Strict equality is…
Q2. Spaceship operator is…
Q3. Null-coalesce operator is…

Discussion

Loading…