The match expression was introduced in PHP 8.0.
It compares a value against several possible conditions
and returns the result associated with the first matching
condition. It is an alternative to many simple
switch statements.
A match expression compares a subject
with different values using strict comparison.
It returns the result of the matching arm.
Syntax:
$result = match ($value) {
value1 => result1,
value2 => result2,
default => defaultResult,
};
Example:
<?php
$day = 3;
$result = match ($day) {
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
default => "Weekend or invalid day",
};
echo $result;
?>
Output:
Wednesday
Since $day is 3, the expression returns
"Wednesday".
Unlike a traditional PHP switch,
match compares values using strict
comparison, similar to ===.
Both value and type must match.
<?php
$value = 10;
$result = match ($value) {
10 => "Integer ten",
"10" => "String ten",
default => "No match",
};
echo $result;
?>
Output:
Integer ten
The integer 10 matches the integer arm.
The string "10" is a different type
and does not match the integer value.
The default arm handles values that
do not match any other arm.
<?php
$color = "Purple";
$result = match ($color) {
"Red" => "Color is Red",
"Blue" => "Color is Blue",
"Green" => "Color is Green",
default => "Color not found",
};
echo $result;
?>
Output:
Color not found
If no arm matches and no default arm exists,
PHP throws an UnhandledMatchError.
Include default when unmatched values
are possible and should be handled.
You can separate multiple values with commas when they should produce the same result.
<?php
$day = "Sunday";
$result = match ($day) {
"Saturday", "Sunday" => "Weekend",
"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday" => "Working day",
default => "Invalid day",
};
echo $result;
?>
Output:
Weekend
Both Saturday and Sunday return the same result. This avoids repeating the same expression.
A match expression can select a grade message based on a grade value.
<?php
$grade = "B";
$result = match ($grade) {
"A" => "Excellent",
"B" => "Very Good",
"C" => "Good",
"D" => "Needs Improvement",
"F" => "Fail",
default => "Invalid grade",
};
echo $result;
?>
Output:
Very Good
Notice that match returns a value.
You can store that value in a variable or use it
directly in another expression.
The right-hand side of each arm can contain an expression, including a calculation.
<?php
$a = 20;
$b = 5;
$operator = "*";
$result = match ($operator) {
"+" => $a + $b,
"-" => $a - $b,
"*" => $a * $b,
"/" => $b != 0
? $a / $b
: "Cannot divide by zero",
default => "Invalid operator",
};
echo $result;
?>
Output:
100
The selected arm calculates the multiplication result. The division arm also checks for division by zero before calculating.
A match expression can return Boolean values or other data types.
<?php
$status = "active";
$isActive = match ($status) {
"active" => true,
"inactive" => false,
default => null,
};
var_dump($isActive);
?>
Output:
bool(true)
The result can be used in conditions or passed to other functions.
You can use a match expression directly as a function argument instead of assigning it to a separate variable first.
<?php
$month = 4;
echo match ($month) {
1 => "January",
2 => "February",
3 => "March",
4 => "April",
5 => "May",
6 => "June",
default => "Other month",
};
?>
Output:
April
PHP evaluates the match expression first and
passes the resulting string to echo.
| Match | Switch |
|---|---|
| Introduced in PHP 8.0. | Available in older PHP versions. |
| Returns a value. | Executes statements. |
| Uses strict comparison. | Uses loose comparison. |
| Does not fall through to later arms. | Can fall through if break is omitted. |
| Uses commas to group values. | Uses multiple case labels to group values. |
| Requires a matching arm or default. | Can finish without a matching case. |
Example comparison:
<?php
$value = 5;
$message = match ($value) {
1 => "One",
5 => "Five",
default => "Other",
};
echo $message;
?>
Output:
Five
Use match when you want to map values
to results. Use if-else for ranges
and complex Boolean conditions.
Write PHP programs to complete these exercises:
Sample starting code:
<?php
$choice = 2;
$result = match ($choice) {
1 => "Home",
2 => "About",
3 => "Contact",
default => "Invalid choice",
};
echo $result;
?>
Expected output:
About
1. In which PHP version was match introduced?
2. Which comparison rule does match use?
3. What happens if no arm matches and there is no default arm?