Match Expressions
Prerequisites: Conditionals
PHP 8's match produces a value directly, using strict
comparison, with no fall-through and no break needed:
$day = 3;
$name = match ($day) {
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
default => 'Unknown',
};
echo $name; // Wednesday
The older switch statement still exists and uses loose (==)
comparison — match is preferred in modern PHP for exactly this
reason.
Common mistakes
- Forgetting
matchrequires an exhaustivedefault(or every possible value covered) — an unmatched value throwsUnhandledMatchErrorinstead of silently doing nothing.