PHP Match Expression vs Switch Statement
PHP 8 introduced the match expression as a modern, more
powerful alternative to the traditional switch statement.
While both structures allow you to control the flow of your program
based on matching value conditions, they differ fundamentally in how
they compare values, return results, and handle syntax. This article
outlines the key differences between match and
switch to help you write cleaner and safer PHP code.
1. Expression vs. Statement (Return Values)
The most fundamental difference is that match is an
expression, whereas switch is a
statement.
Because match is an expression, it evaluates to a value.
This means you can assign the result of a match block
directly to a variable, return it from a function, or pass it as an
argument.
// Using match (returns a value directly)
$statusDescription = match ($statusCode) {
200 => 'OK',
404 => 'Not Found',
500 => 'Internal Server Error',
};
// Using switch (requires manual assignment within cases)
switch ($statusCode) {
case 200:
$statusDescription = 'OK';
break;
case 404:
$statusDescription = 'Not Found';
break;
case 500:
$statusDescription = 'Internal Server Error';
break;
}2. Strict Comparison vs. Loose Comparison
Safety is a major advantage of the match expression.
matchuses strict comparison (===): It compares both the value and the type.switchuses loose comparison (==): It compares only the value after type coercion, which can lead to unexpected bugs and security vulnerabilities.
$value = '100';
// Match will not match because '100' (string) is not === 100 (integer)
$result = match ($value) {
100 => 'This is an integer',
default => 'No match found', // This will execute
};
// Switch will match because '100' == 100 evaluates to true
switch ($value) {
case 100:
$result = 'This is an integer'; // This executes
break;
}3. Fallthrough and the
break Keyword
In a switch statement, if you forget to write a
break keyword at the end of a case block, PHP
will continue executing the subsequent cases (known as “fallthrough”).
This is a common source of programming errors.
The match expression does not support fallthrough. It
only executes the single matching arm and then implicitly breaks. If you
want multiple values to trigger the same outcome, you can separate them
with commas on a single line.
// Combining conditions in match
$role = match ($userType) {
'admin', 'super_admin' => 'Administrator',
'editor', 'author' => 'Content Creator',
default => 'Guest',
};4. Exhaustiveness (Unhandled Values)
The match expression is exhaustive. If the value you are
testing does not match any of the specified arms, and you have not
provided a default arm, PHP will throw an
UnhandledMatchError. This forces you to handle all possible
inputs safely.
In contrast, a switch statement will silently ignore
unmatched values and continue executing the rest of the script, which
can result in undefined variables later in your code.
$color = 'green';
// Throws UnhandledMatchError because 'green' is not handled
try {
$result = match ($color) {
'red' => '#FF0000',
'blue' => '#0000FF',
};
} catch (\UnhandledMatchError $e) {
// Error caught here
}Summary Comparison
| Feature | switch Statement |
match Expression |
|---|---|---|
| Type of Structure | Statement | Expression (evaluates to a value) |
| Comparison Type | Loose (==) |
Strict (===) |
| Return Value | No direct return value | Returns the matched arm’s value |
| Control Flow | Requires manual break |
Automated break (no fallthrough) |
| Multiple Matches | Uses consecutive case
statements |
Comma-separated values on one line |
| Exhaustiveness | Silently ignores unhandled cases | Throws UnhandledMatchError if
unhandled |