Switch vs Match
// switch — loose, fall-through, no return value
switch ($status) {
case 'active': $label = 'Active'; break;
case 'banned': $label = 'Banned'; break;
default: $label = 'Unknown';
}
// match — strict, no fall-through, returns value
$label = match($status) {
'active' => 'Active',
'banned' => 'Banned',
'pending', 'unverified' => 'Waiting', // multiple conditions
default => 'Unknown',
};
Advantages
- Strict comparison:
match(0) ≠ match('foo')
- If no arm matches and there is no
default, an UnhandledMatchError is thrown
- More compact, readable code