PHP 8 News: Match Expression

Antonio Silva - Mar 1 - - Dev Community

Match Expression

In PHP 8, Match Expression is introduced as a new control structure that provides a more concise and readable alternative to the traditional switch statement. It's designed to make code more expressive and easier to write, especially when dealing with simple value matching.

Syntax

The syntax for a Match Expression looks like this:

$result = match ($value) {
    'option1' => $result1,
    'option2' => $result2,
    default => $defaultResult,
};
Enter fullscreen mode Exit fullscreen mode

Comparison to Switch

While a switch statement works with loose comparisons (==), a match expression works with strict comparisons (===). This means that not only the value, but also the type of the value must match for the branch to be executed.

Value Matching

Each branch of the match expression compares the given value against a specific pattern or value. If there's a match, the corresponding expression on the right side of the arrow (=>) is evaluated and returned.

Default Branch

Similar to switch, you can have a default branch that will be executed if none of the other branches match the value.

Return Value

The overall value of the match expression is the value of the expression on the right side of the arrow in the matched branch. If no branch matches and there's no default branch, the match expression evaluates to null.

Expression on the Right Side

The expression on the right side of the arrow in each branch can be any valid PHP expression, including function calls, variable assignments, or even another match expression.

Here's a simple example demonstrating the usage of match expression:

$value = 'option2';

$result = match ($value) {
    'option1' => 'Result for option 1',
    'option2' => 'Result for option 2',
    default => 'Default result',
};

echo $result; // Output: Result for option 2
Enter fullscreen mode Exit fullscreen mode

In this example, the match expression checks the value of $value against different options. Since $value is option2, the corresponding branch option2 => Result for option 2 is matched, and Result for option 2 is assigned to $result.

Matching against different data types

$value = 42;

$result = match ($value) {
    42 => 'The answer to everything',
    '42' => 'A string containing 42',
    default => 'Something else',
};

echo $result; // Output: The answer to everything
Enter fullscreen mode Exit fullscreen mode

In this example, even though '42' is a string and 42 is an integer, PHP's strict comparison in match expressions ensures that only the branch with the matching type and value is executed.

Using expressions on the right side

$value = 5;

$result = match ($value) {
    1 => 'One',
    2 => 'Two',
    default => 'Greater than two: ' . ($value * 10),
};

echo $result; // Output: Greater than two: 50
Enter fullscreen mode Exit fullscreen mode

Here, the default branch includes an expression ($value * 10), which multiplies the value of $value by 10 and concatenates it with the string 'Greater than two: '.

Combining match expressions

$value = 8;

$result = match ($value) {
    1 => 'One',
    2 => 'Two',
    default => match (true) {
        $value > 5 => 'Greater than five',
        $value > 2 => 'Greater than two',
        default => 'Something else',
    },
};

echo $result; // Output: Greater than five
Enter fullscreen mode Exit fullscreen mode

In this example, the default branch of the outer match expression itself contains another match expression to handle more complex cases.

These examples illustrate how match expressions in PHP 8 can be used for concise and expressive value matching in various scenarios. They provide a flexible alternative to traditional switch statements and can lead to cleaner and more readable code.

Match expressions can help in writing cleaner and more readable code, especially when dealing with multiple conditions and straightforward value comparisons. They provide a more modern and concise alternative to the traditional switch statement in PHP.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .