Conditionals in PHP

From the PHP cheat sheet · Control Structures · verified Jul 2026

Conditionals

If statements, switch, and match expressions

php
// If statement
if ($age >= 18) {
    echo "Adult";
} elseif ($age >= 13) {
    echo "Teen";
} else {
    echo "Child";
}

// Ternary operator
$status = $age >= 18 ? 'adult' : 'minor';

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

// Match expression (PHP 8+)
$result = match($status) {
    'pending' => 'Waiting',
    'active' => 'Ready',
    'closed' => 'Done',
    default => 'Unknown'
};
💡 Match is stricter than switch (no type coercion)
⚡ Null coalescing (??) simplifies default values
📌 Match returns values, switch executes code
🔥 Alternative syntax cleaner for templates

More PHP tasks

Back to the full PHP cheat sheet