The match expression is a cleaner alternative to switch, introduced in PHP 8. It uses strict comparison, returns a value directly, and throws an error if no arm matches. It prevents accidental fall-through bugs.
Match Expression (PHP 8)
// Old way — switch
switch ($status) {
case "active": $label = "Active"; break;
case "inactive": $label = "Inactive"; break;
default: $label = "Unknown";
}
// New way — match (strict comparison, no fallthrough)
$label = match($status) {
"active" => "Active",
"inactive" => "Inactive",
"banned" => "Banned",
default => "Unknown"
};
// Multiple conditions per arm
$discount = match(true) {
$total >= 500 => 0.20,
$total >= 200 => 0.10,
$total >= 100 => 0.05,
default => 0.00,
};
// Throws UnhandledMatchError if no match and no default