With the match case distinction, PHP 8 actually brings a comparatively large innovation to basic programming functions.
The aim here is to achieve better structured and more readable source code in as few lines as possible. For many applications, match is intended to replace switch, which is not very popular among professionals.
In contrast to switch, match returns a result for each case. This means that in the end there is a result that can be used for further processing. From PHP's point of view, match is therefore an expression as it has a value. This is why there must always be a semicolon at the end of a match statement after the closing curly bracket:
Result = match (variable) {
value1 => return 1,
value2, value3 => return 2,
default: standard return
};
But this is not the only difference between match and switch. The syntax is also clearly different, as you can see here. Although the variable is enclosed in round brackets as usual, each value check is followed by the return on just one line. The lines are separated by commas. match does not use break. Instead, the check is ended directly as soon as the first value matches the current value of the variable.
If a return applies to several values, the values in the respective line can be separated by commas. Finally, the default standard case is optional. It is returned if no values before it apply.
Note: The default case is optional; that is, it does not necessarily have to be defined. However, a little caution is required here, as match is very strict. If none of the values apply and there is no default case, then match throws an error of type UnhandledMatchError. The purpose of this strictness is to avoid small bugs by using clean code right from the start.
Now take a look at the following example.
$age = 55;
$result = match ($age) {
54 => "You are 54.",
55 => "You are 55.",
56, 57, 58 => "You are between 56 and 58.",
default => "You do not fit into any of the categories."
};
echo $result;
In this case, the match case distinction first checks for the value 54. As it does not apply, it jumps to the value 55 for checking. This corresponds to the current value of the $age variable. The return from this line is then used and assigned to the $result variable. The output is therefore the following:
You are 55.
The next check for multiple values and the standard case are no longer executed.
One important difference from switch remains: when checking the values, match works with exact equality—that is, corresponds to the operator === and includes the type of the value. To test this, we can change the first line of the script and turn the $age variable into a string instead of an integer.
$age = '55';
$result = match ($age) {
54 => "You are 54.",
55 => "You are 55.",
56, 57, 58 => "You are between 56 and 58.",
default => "You do not fit into any of the categories."
};
echo $result;
In this case, match detects a type mismatch for all values. Therefore, the default case occurs and the output reads as follows:
You do not fit into any of the categories.
In the previous examples, match was used for simple value checks. However, more complex checks are also conceivable. In this case, the truth value true is used as the variable:
$result = match (true) {
In the respective line for this case, we then replace the value with a check that returns a truth value as the result:
$age >= 10 && $age < 30 => "You are between 10 and 29.",
As soon as this check returns true for a line, match strikes and accepts the return value for this line—that is, for the following line in the example:
$age >= 50 && $age < 70 => "You are between 50 and 69.",
Note: The actual test is moved to the test line here. This also means that you can test at this point without strict typing. In our case, >= and < are used as operators. This means that the check would also return true if $age were a string instead of an integer.
The following lines are ignored, even if their checks would also return true.
$age = 55;
$result = match (true) {
$age >= 10 && $age < 30 => "You are between 10 and 29.",
$age >= 30 && $age < 50 => "You are between 30 and 49.",
$age >= 50 && $age < 70 => "You are between 50 and 69.",
default => "You do not fit into any of the categories."
};
echo $result;
Note: When planning the match case differentiation, some function enhancements were already under discussion, such as statement blocks in addition to the single-line return values. We can therefore expect a number of new features in the next PHP versions.
Editor’s note: This post has been adapted from a section of the book PHP and MySQL: The Comprehensive Guide by Christian Wenz and Tobias Hauser. Christian is a consultant and trainer who specializes in web technologies and web security. He leads digitization projects in corporate environments. His books have been translated into more than a dozen languages. Tobias is a consultant, trainer, and author. He supports companies with his focus on PHP-based web applications, covering everything from system selection to interface architecture, and writes regularly about web topics.
This post was originally published 5/2025.