The match expression, introduced in PHP 8.0, offers a modern way to handle multiple conditional checks.
The new match expression in PHP 8 is a control flow structure that matches a given subject expression with one or more alternative conditional expressions using identity comparison and returns a value from the matched branch.
Match expression syntax is one of the nicest features in PHP 8 that improves the switch syntax in multiple ways.
Key Differences Between Match and Switch
Both switch and match in PHP are used to check conditions and run code. But there are some important differences:
Strict Comparison (===)
- In
switch, PHP uses loose comparison (==).
Example:switchwill treat"5"(string) and5(number) as the same. - In
match, PHP uses strict comparison (===).
Example:"5"(string) and5(number) are not the same.
No Need of break
- In
switch, you must writebreakafter every case, otherwise PHP will continue to the next case. - In
match, no need to writebreak. Only the matched condition runs.
Match Can Return a Value Directly
switchonly executes code, you need extra lines to store a result.matchcan return values directly and can be used in expressions.
More Compact and Clean
switchcode is usually longer with multiple cases and breaks.matchmakes the same logic short and clean.
Example: Switch vs Match
Using switch
<?php
$number = 2;
switch ($number) {
case 1:
$result = "One";
break;
case 2:
$result = "Two";
break;
default:
$result = "Other";
}
echo $result; // Output: Two
?>
Using match
<?php
$number = 2;
$result = match ($number) {
1 => "One",
2 => "Two",
default => "Other",
};
echo $result; // Output: Two
?>
PHP 8 Match vs Switch: Quick Comparison
| Feature | Switch | Match |
|---|---|---|
| Comparison | Uses loose comparison (==) | Uses strict comparison (===) |
| Break Required | Yes, must write break after each case | No break needed |
| Return Value | Cannot return directly, need extra code | Can return a value directly |
| Code Length | Longer and repetitive | Short and clean |
| Multiple Matches | Runs only one case (unless break missing) | Can map multiple values to one result |
| Default Case | Optional, but used for unmatched cases | Mandatory if all cases are not covered |
| Error Chances | High (if you forget break) | Low (no break needed) |
Example #1 Structure of a match expression
<?php
$return_value = match (subject_expression) {
single_conditional_expression => return_expression,
conditional_expression1, conditional_expression2 => return_expression,
};
?>
Example #2 Basic match usage
<?php
$food = 'cake';
$return_value = match ($food) {
'apple' => 'This food is an apple',
'bar' => 'This food is a bar',
'cake' => 'This food is a cake',
};
var_dump($return_value);
?>
output: string(19) "This food is a cake"
Example #3 Example of using match with comparison operators
<?php
$age = 18;
$output = match (true) {
$age < 2 => "Baby",
$age < 13 => "Child",
$age <= 19 => "Teenager",
$age >= 40 => "Old adult",
$age > 19 => "Young adult",
};
var_dump($output);
?>
output: string(8) "Teenager"