Using match Expression in PHP 8.0

Using match Expression in PHP 8.0

PHP has the switch statement that can be used to execute different blocks of code based on different cases. Since PHP 8.0, we can use the match expression that is similar to switch statement.

Let's say we have the switch statement which prints a message based on error code.

<?php

$errorCode = 2;
switch ($errorCode) {
    case 1:
        echo 'Access denied';
        break;
    case 2:
        echo 'Incorrect password';
        break;
    case 3:
        echo 'Invalid format';
        break;
}

This code can be rewritten using the match expression as follows:

<?php

$errorCode = 2;
echo match ($errorCode) {
    1 => 'Access denied',
    2 => 'Incorrect password',
    3 => 'Invalid format',
};

There are differences between match expression and switch statement:

  • A match expression returns a value. When using the switch statement, we have to assign a value to a variable in each block. Note that it is not necessary to use a value returned by a match expression.
<?php

$number = 2;
$result = match ($number) {
    1 => 'One',
    2 => 'Two',
    3 => 'Three',
};
echo $result; // Two
<?php

$number = 2;
switch ($number) {
    case 1:
        $result = 'One';
        break;
    case 2:
        $result = 'Two';
        break;
    case 3:
        $result = 'Three';
        break;
}
echo $result; // Two
  • A match expression uses strict comparison (===) rather than loose comparison (==) as the switch statement does.
<?php

$number = '2';
$result = match ($number) {
    1 => 'One',
    2 => 'Two',
    default => 'Unknown',
};
echo $result; // Unknown
<?php

$number = '2';
switch ($number) {
    case 1:
        $result = 'One';
        break;
    case 2:
        $result = 'Two';
        break;
    default:
        $result = 'Unknown';
        break;
}
echo $result; // Two
<?php

$number = 4;
$result = match ($number) {
    1 => 'One',
    2 => 'Two',
    3 => 'Three',
};
// Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type int in main.php:4
<?php

$result = 'Unknown';
$number = 4;
switch ($number) {
    case 1:
        $result = 'One';
        break;
    case 2:
        $result = 'Two';
        break;
    case 3:
        $result = 'Three';
        break;
}
echo $result; // Unknown
  • A match expression allows only a single expression rather than a block of code as the switch statement does.
<?php

$number = 2;
$result = match ($number) {
    1 => ($number + 1) * ($number + 1) * 1,
    2 => ($number + 2) * ($number + 2) * 2,
    3 => ($number + 3) * ($number + 3) * 3,
};
echo $result; // 32
<?php

$number = 2;
switch ($number) {
    case 1:
        $temp = $number + 1;
        $result = $temp * $temp * 1;
        break;
    case 2:
        $temp = $number + 2;
        $result = $temp * $temp * 2;
        break;
    case 3:
        $temp = $number + 3;
        $result = $temp * $temp * 3;
        break;
}
echo $result; // 32
  • A match expression not fall-through to later cases because each case has an implicit break. A switch statement fall-through to later cases, and we must add break explicitly.
<?php

$number = 2;
$result = match ($number) {
    1 => 'One',
    2 => 'Two',
    3 => 'Three',
};
echo $result; // Two
<?php

$number = 2;
switch ($number) {
    case 1:
        $result = 'One';
    case 2:
        $result = 'Two';
    case 3:
        $result = 'Three';
}
echo $result; // Three

There are also similarities between match expression and switch statement:

  • A match expression and switch statement can have the default case. It is executed if none of the other cases matched. Note that the order of default case doesn't matter.
<?php

$number = 3;
$result = match ($number) {
    1 => 'One',
    2 => 'Two',
    default => 'Unknown',
};
echo $result; // Unknown
<?php

$number = 3;
switch ($number) {
    case 1:
        $result = 'One';
        break;
    case 2:
        $result = 'Two';
        break;
    default:
        $result = 'Unknown';
        break;
}
echo $result; // Unknown
  • A match expression can contain multiple conditions separated by a comma (,) where the same block of code is executed. It is similar to empty cases in the switch statement, where control is passed to the next case.
<?php

$number = 1;
$result = match ($number) {
    1, 2 => 'One and two',
    3 => 'Three',
};
echo $result; // One and two
<?php

$number = 2;
switch ($number) {
    case 1:
    case 2:
        $result = 'One and two';
        break;
    case 3:
        $result = 'Three';
        break;
}
echo $result; // One and two

There are some notes for match expression:

  • A match expression need to be terminated with a semicolon (;).
<?php

match (2) {
    1 => 'One',
    2 => 'Two',
    3 => 'Three',
};
  • A trailing comma in the match expression is optional.
<?php

match (2) {
    1 => 'One',
    2 => 'Two',
    3 => 'Three'
};

Leave a Comment

Cancel reply

Your email address will not be published.