Match¶
The match
expression is the remote cousin of the switch
. It takes a value, branch among a list of cases, or fallback to a default one, and returns a unique value.
match
use a strict comparison for the comparison. match
raise an error if no case match, unless the default
case is provided.
match
is an expression, to be used in an assignment, while switch
is a command, and must be used alone.
<?php
$return_value = match ($food) {
'apple' => 'This food is an apple',
'bar' => 'This food is a bar',
'cake' => 'This food is a cake',
default => 'This is not food'
};
?>
See also PHP Tricks: Multi-value match()
Related : Switch, Default, If Then Else, UnhandledMatchError, Case, Comparison, Conditional Structures, Control Flow, Switch Fallthrough, Simple Switch, Strict Comparison, Switch Case, Switch Default
Added in PHP 8.0