A side-by-side comparison showing why match is preferable for most branching logic.
$status = 2;
// ── Old switch ────────────────────────────────────────────────────
switch ($status) {
case 1:
$label = 'Active';
break;
case 2:
$label = 'Pending';
break;
default:
$label = 'Unknown';
}
// ── PHP 8 match ───────────────────────────────────────────────────
$label = match($status) {
1 => 'Active',
2 => 'Pending',
3, 4 => 'Suspended', // multiple conditions on one arm
default => 'Unknown',
};
// match uses strict (===) comparison — no type juggling
$value = "1";
echo match(true) {
is_numeric($value) && $value > 0 => 'Positive number',
is_string($value) => 'String',
default => 'Other',
};
// Throws UnhandledMatchError if no arm matches (unlike switch which silently continues)
All Comments