

Using match Expression in PHP 8.0
PHP has switch
statement that can be used to execute different blocks of code based on different cases. Since PHP 8.0, we can use match
expression that is similar to switch
statement.
Let's say we have switch
statement which prints message based on error code.
<?php
$errorCode = 2;
switch ($errorCode) {
case 1:
echo 'Access denied';
break;
case 2:
echo 'Incorrect password';
break;
case 3:
echo 'Invalid format';
break;
}
This code can be rewritten using match
expression as follows:
<?php
$errorCode = 2;
echo match ($errorCode) {
1 => 'Access denied',
2 => 'Incorrect password',
3 => 'Invalid format',
};
There are differences between match
expression and switch
statement:
- A
match
expression returns a value. When usingswitch
statement we have to assign value to a variable in each block. Note that it is not necessary to use a value returned by amatch
expression.
<?php
$number = 2;
$result = match ($number) {
1 => 'One',
2 => 'Two',
3 => 'Three',
};
echo $result; // Two
<?php
$number = 2;
switch ($number) {
case 1:
$result = 'One';
break;
case 2:
$result = 'Two';
break;
case 3:
$result = 'Three';
break;
}
echo $result; // Two
- A
match
expression uses strict comparison (===
) rather than loose comparision (==
) as theswitch
statement does.
<?php
$number = '2';
$result = match ($number) {
1 => 'One',
2 => 'Two',
default => 'Unknown',
};
echo $result; // Unknown
<?php
$number = '2';
switch ($number) {
case 1:
$result = 'One';
break;
case 2:
$result = 'Two';
break;
default:
$result = 'Unknown';
break;
}
echo $result; // Two
- A
match
expression throws anUnhandledMatchError
if no case match is found.
<?php
$number = 4;
$result = match ($number) {
1 => 'One',
2 => 'Two',
3 => 'Three',
};
// Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type int in main.php:4
<?php
$result = 'Unknown';
$number = 4;
switch ($number) {
case 1:
$result = 'One';
break;
case 2:
$result = 'Two';
break;
case 3:
$result = 'Three';
break;
}
echo $result; // Unknown
- A
match
expression allows only single expression rather than block of code as theswitch
statement does.
<?php
$number = 2;
$result = match ($number) {
1 => ($number + 1) * ($number + 1) * 1,
2 => ($number + 2) * ($number + 2) * 2,
3 => ($number + 3) * ($number + 3) * 3,
};
echo $result; // 32
<?php
$number = 2;
switch ($number) {
case 1:
$temp = $number + 1;
$result = $temp * $temp * 1;
break;
case 2:
$temp = $number + 2;
$result = $temp * $temp * 2;
break;
case 3:
$temp = $number + 3;
$result = $temp * $temp * 3;
break;
}
echo $result; // 32
- A
match
expression not fall-through to later cases because each case has an implicitbreak
. Aswitch
statement fall-through to later cases and we must addbreak
explicitly.
<?php
$number = 2;
$result = match ($number) {
1 => 'One',
2 => 'Two',
3 => 'Three',
};
echo $result; // Two
<?php
$number = 2;
switch ($number) {
case 1:
$result = 'One';
case 2:
$result = 'Two';
case 3:
$result = 'Three';
}
echo $result; // Three
There are also similarities between match
expression and switch
statement:
- A
match
expression andswitch
statement can have thedefault
case. It is executed if none of the other cases matched. Note that the order ofdefault
case doesn't matter.
<?php
$number = 3;
$result = match ($number) {
1 => 'One',
2 => 'Two',
default => 'Unknown',
};
echo $result; // Unknown
<?php
$number = 3;
switch ($number) {
case 1:
$result = 'One';
break;
case 2:
$result = 'Two';
break;
default:
$result = 'Unknown';
break;
}
echo $result; // Unknown
- A
match
expression can contain multiple conditions separated by a comma (,
) where the same block of code is executed. It is similar to empty cases inswitch
statement where control is passed for the next case.
<?php
$number = 1;
$result = match ($number) {
1, 2 => 'One and two',
3 => 'Three',
};
echo $result; // One and two
<?php
$number = 2;
switch ($number) {
case 1:
case 2:
$result = 'One and two';
break;
case 3:
$result = 'Three';
break;
}
echo $result; // One and two
There are some notes for match
expression:
- A
match
expression need to be terminated with a semicolon (;
).
<?php
match (2) {
1 => 'One',
2 => 'Two',
3 => 'Three',
};
- A trailing comma in
match
expression is optional.
<?php
match (2) {
1 => 'One',
2 => 'Two',
3 => 'Three'
};