Theory
Operators
PHP 8.3.8
Core Concepts

Operators

Topic 3 of 13
PHP
<?php
// Arithmetic
$a = 10;  $b = 3;
echo $a ** $b;  // 1000  (power)
echo $a %  $b;  // 1     (modulo)
echo 10 / 3;   // 3.333โ€ฆ

// Comparison โ€” ALWAYS prefer ===
$x ==  $y;   // equal (loose โ€” coerces types)
$x === $y;   // identical (value + type)
$x <=> $y;   // spaceship: returns -1, 0, or 1

// Logical
$a && $b;    // AND (short-circuit)
$a || $b;    // OR  (short-circuit)
!$a;        // NOT

// String concatenation
$full = "Hello" . " World";
$full .= "!";  // append

// Null coalescing (PHP 7+)
$user = $_GET['user'] ?? 'guest';

// Null coalescing assignment (PHP 7.4+)
$config['debug'] ??= false;

// Ternary shorthand (Elvis)
$label = $name ?: 'Anonymous';

// Spaceship โ€” perfect for sorting
usort($arr, fn($a, $b) => $a <=> $b);
โš ๏ธ Pitfall
0 == "foo" returned true in PHP 7 (coercion). PHP 8 fixed this โ€” now false. Always use === to be safe in all versions.