PHP match Expression
The match expression was added in PHP 8.0. It compares one value with many options, and returns the result of the first option that matches.
Many developers see it as a shorter and safer version of the switch statement. You do not need break, and it gives you a value directly that you can store in a variable.
Syntax
<?php
$result = match ($value) {
option1 => result1,
option2, option3 => result2,
default => default_result,
};
?>
- PHP checks the value against each option from top to bottom.
- The result of first matching option is returned.
- Use a comma between options to check more than one value for the same result.
- The default option is optional, but it is good to use.
- A semicolon is required at the end because match is an expression, not a statement.
Examples
Example #1: Day number to day name
In this example we have a day number. The match expression finds the matching number and returns the day name.
<?php
$day = 3;
$name = match ($day) {
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
default => "Unknown day",
};
echo $name;
?>
Output:
Wednesday
Example #2: Multiple values for one result
When many values should give the same result, separate them with a comma.
<?php
$day = 6;
$type = match ($day) {
1, 2, 3, 4, 5 => "Weekday",
6, 7 => "Weekend",
default => "Invalid day",
};
echo $type;
?>
Output:
Weekend
Example #3: Using match(true) for ranges
Match cannot use conditions like >= directly. A common trick is to write match (true) and put the conditions as options. PHP returns the result of first condition that is true.
<?php
$marks = 82;
$grade = match (true) {
$marks >= 90 => "A",
$marks >= 75 => "B",
$marks >= 50 => "C",
default => "Fail",
};
echo $grade;
?>
Output:
B
match vs switch
Both are used for decision making, but they do not work in same way.
| Feature | switch | match |
|---|---|---|
| Comparison | Loose (==) | Strict (===) |
| Returns a value | No | Yes |
| break needed | Yes | No |
| No option matched | Does nothing silently | Throws UnhandledMatchError |
| Code per option | Many statements | One expression only |
Strict comparison example
The value below is a string “1”. With switch, it would match case 1 because switch uses loose comparison. The match expression checks the type also, so only the string option matches.
<?php
$value = "1";
echo match ($value) {
1 => "Integer one",
"1" => "String one",
default => "No match",
};
?>
Output:
String one
What if nothing matches?
If no option matches and there is no default, PHP throws an UnhandledMatchError. So it is better to add default, unless you really want the error.
<?php
$x = 10;
echo match ($x) {
1 => "One",
2 => "Two",
};
// Throws UnhandledMatchError because 10 is not handled
?>
Summary
- match was added in PHP 8.0.
- It returns a value, so you can store it in a variable.
- It uses strict comparison (===).
- No break is needed.
- Use default to avoid UnhandledMatchError.
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |