TIL match expression (like switch)
POSTED ON:
TAGS: php clean-code
The new match expression of PHP is very similar to the switch statement, except it is an expression and can be used to directly assign values to a variable or return values.
$fontWeight = match ($weight) {
100 => "Super Thin",
300 => "Thin",
400 => "Normal",
600 => "Bold",
900 => "Heavy"
};
$return_value = match ($food) {
'apple' => 'This food is an apple',
'bar' => 'This food is a bar',
'cake' => 'This food is a cake',
};
REFERENCE:
PHP 8 features I wish also existed in JavaScript
php manual
Related TILs
Tagged: php