![]() |
VOOZH | about |
The PHP match expression is used for the identity check of a value. It is similar to the switch statement i.e. it matches the expression with its alternative values. The match expressions are available in PHP 8.0.0.
The match expression compares the value using a strict comparison operator (===) whereas the switch statement uses a loose comparison operator (==).
Syntax:
return_value = match(expr) {
key1 => val1,
key2 => val2,
...
}
Note: The match expression must be ended with semicolons.
Example 1: The following code demonstrates the match expression in PHP.
Output:
string(10) "PHP Course"
Example 2: The following code is another example of a PHP match expression.
Output:
string(11) "Distinction"
Reference: https://www.php.net/manual/en/control-structures.match.php