Match Expressions
match is what switch should have been. It uses strict comparison, returns a value, doesn't fall through, and throws an error if no arm matches. It's an expression, not a statement.
// switch: verbose, fall-through prone, loose comparison
switch ($statusCode) {
case 200:
case 201:
$text = 'Success';
break;
case 404:
$text = 'Not Found';
break;
default:
$text = 'Unknown';
break;
}
// match: concise, strict, returns a value
$text = match($statusCode) {
200, 201 => 'Success',
404 => 'Not Found',
500 => 'Server Error',
default => 'Unknown',
};
// No expression arms — use match(true) for conditions
$category = match(true) {
$age < 13 => 'child',
$age < 18 => 'teenager',
$age < 65 => 'adult',
default => 'senior',
};
The fact that match throws UnhandledMatchError when no arm matches is a feature, not a bug. It forces you to handle all cases explicitly, catching logic errors at runtime instead of silently producing wrong results.
Significance: Safety
Every
switch fall-through bug that ever shipped to production was a consequence of switch's design. match makes the common case (no fall-through, strict comparison, return a value) the default, and eliminates the break ceremony entirely.