How to use php 8 match

PHP 8 is a version of the PHP programming language that was released in November 2020. The match keyword is a new feature in PHP 8 that allows you to perform pattern matching on values. It is similar to a switch statement, but it is more powerful and expressive.
Here is an example of how to use the match keyword in PHP 8:
$value = 'apple';
$result = match ($value) {
'apple' => 'This is an apple',
'banana' => 'This is a banana',
'orange' => 'This is an orange',
default => 'This is some other fruit',
};
echo $result; // Outputs: "This is an apple"
In the example above, the value of the $value variable is compared to the different case labels ('apple', 'banana', 'orange'). If a match is found, the corresponding expression is evaluated and returned. If no match is found, the default case is used. The default case is optional and can be used to provide a default value if none of the other cases match.
You can also use patterns in the case labels to match more complex values. For example:
$value = ['name' => 'Alice', 'age' => 30];
$result = match ($value) {
['name' => 'Alice', 'age' => $age] => "This person is named Alice and is $age years old",
['name' => $name, 'age' => $age] => "This person is named $name and is $age years old",
default => 'This is some other person',
};
echo $result; // Outputs: "This person is named Alice and is 30 years old"
In the example above, the first case label uses a pattern to match an array with two elements: 'name' and 'age'. The variables $name and $age are extracted from the array and used in the expression. The second case label uses a similar pattern, but does not specify the values for 'name' and 'age', so it will match any array with these elements. The default case is used if none of the other cases match.
-
Date: