PHP has the switch statement that can be used to execute different blocks of code based on different cases. Since PHP 8.0, we can use the match expression that is similar to switch statement.
Let's say we have the switch statement which prints a 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 the 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
matchexpression returns a value. When using theswitchstatement, we have to assign a value to a variable in each block. Note that it is not necessary to use a value returned by amatchexpression.
<?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
matchexpression uses strict comparison (===) rather than loose comparison (==) as theswitchstatement 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
matchexpression throws anUnhandledMatchErrorif 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
matchexpression allows only a single expression rather than a block of code as theswitchstatement 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
matchexpression not fall-through to later cases because each case has an implicitbreak. Aswitchstatement fall-through to later cases, and we must addbreakexplicitly.
<?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
matchexpression andswitchstatement can have thedefaultcase. It is executed if none of the other cases matched. Note that the order ofdefaultcase 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
matchexpression can contain multiple conditions separated by a comma (,) where the same block of code is executed. It is similar to empty cases in theswitchstatement, where control is passed to 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
matchexpression need to be terminated with a semicolon (;).
<?php
match (2) {
1 => 'One',
2 => 'Two',
3 => 'Three',
};
- A trailing comma in the
matchexpression is optional.
<?php
match (2) {
1 => 'One',
2 => 'Two',
3 => 'Three'
};
Leave a Comment
Cancel reply