Table of Contents
Introduction
The match
expression branches evaluation based on an identity check of a value. Similar to a switch
statement, a match
expression has a subject expression that is compared against multiple alternatives. However, unlike switch
, it will evaluate to a value much like a ternary expression. Also, unlike switch, the comparison is an identity check (===
) rather than a weak equality check (==
).
Match expressions are available as of PHP 8.0.0.
Syntax of the match Expression
The syntax of the match
expression is given as:
Syntax
Thematch
expression.
1$return_value = match (subject_expression) {
2 single_conditional_expression => return_expression,
3 conditional_expression1, conditional_expression2 => return_expression,
4};
A match expression must be terminated by a semicolon ;
.
As with switch
statements, match
expressions are executed match arm by match arm. In the beginning, no code is executed. The conditional expressions are only evaluated if all previous conditional expressions failed to match the subject expression. Only the return expression corresponding to the matching conditional expression will be evaluated.
The key differences between a match
expression and a switch
statement:
- A
match
arm compares values strictly (===
) instead of loosely as theswitch
statement does. - Though a
match
expression doesn’t allow for fallthrough conditions, it can combine multiple conditions on the same line, separated by commas. - A
match
expression returns a value. - A
match
expression doesn’t require abreak
statement
ADVERTISEMENT
Using the match Expression
Example
Using thematch
expression.
1<?php
2 $var2 = 2;
3 $var1 = match($var2){
4 1, 2 => 'Weekly',
5 3 => 'Monthly',
6 4, 5 => 'Quarterly',
7 default => 'Annually',
8 };
9 echo "The outcome is $var1.";
10?>
The outcome is Weekly.
If none of the conditional expressions matches the subject expression, the default
return expression is executed.
Example
Using thematch
expression with no matching conditional expression.
1<?php
2 $var2 = 80;
3 $var1 = match($var2){
4 1, 2 => 'Weekly',
5 3 => 'Monthly',
6 4, 5 => 'Quarterly',
7 default => 'Annually',
8 };
9 echo "The outcome is $var1.";
10?>
The outcome is Annually.
It is also possible to use the match
expression to branch on integer ranges.
Example
Using thematch
expression on integer ranges.
1<?php
2 $age = 33;
3 $cat = match (true) {
4 $age >= 65 => 'senior',
5 $age >= 25 => 'adult',
6 $age >= 18 => 'young adult',
7 default => 'kid',
8 };
9 echo "Your age is $age and you are in the $cat category.";
10?>
Your age is 33 and you are in the adult category.
The following example was previously illustrated using the switch
statement. We now rework using the match
expression.
Example
Using thematch
expression on integer ranges.
1<?php
2 $salary = 8000;
3 $KPI = 62;
4 $bonus = match (true){
5 $KPI > 80 => 1000,
6 $KPI > 70 && $KPI <= 80 => 1000,
7 $KPI > 60 && $KPI <= 70 => 500,
8 default => 0,
9 };
10 $salary += $bonus;
11 echo "Your bonus is $bonus. <br>" ;
12 echo "Your updated salary is $salary." ;
13?>
Your bonus is 500.
Your updated salary is 8500.
The match
expression is well-suited for cases where multiple cases result in the same outcome.
Example
Using thematch
expression.
1<?php
2 $month = "May";
3 $daysInMonth = match ($month){
4 "February" => 28,
5 "January", "March", "May", "July", "August", "October", "December" => 31,
6 "April", "June", "September", "November" => 30,
7 default => "invalid month.",
8 };
9 echo "There are $daysInMonth days in the month of $month.";
10?>
There are 31 days in the month of May.