Backend Development

PHP Shortening Switch Statements With PHP 8.0 Match Expression

PHP Shortening Switch Statements With PHP 8

The PHP match expression is a new language expression released in PHP 8.0 that can act as an alternative to switch statements with some differences.

 

 

The Match expression in PHP can be used to evaluate an identity check of a value against multiple alternatives similar to the switch statements. Match expression checks consists of multiple match arms or match branches.

Example: Consider this switch statement

<?php

$animal = 'lion';

switch($animal) {
    case 'lion':
        echo 'This is lion';
        break;
    case 'giraffe':
         echo 'This is giraffe';
         break;
    case 'crocodile':
    	 echo 'This is crocodile';
    	 break;
}

This expression can be rewritten using Match as:

$return = match ($animal) {
    'lion' => 'This is lion',
    'giraffe' => 'This is giraffe',
    'crocodile' => 'This is crocodile'
};

echo $return;

As you see the Match expression is much shorter than switch. Note that match($value) return the matched value as shown in the $return variable unlike switch statement which doesn’t return a value. 

Match expressions can be thought as ternary expression with multiple conditionals. 

 

Differences Between match and switch

  • match expressions return a value.
  • switch statements compares values loosely while match compares values strictly (===). For example:
$score = '10';
$return = match($score) {
    10 => 'You acheived high score',
    5 => 'You acheived medium score',
    default => 'invalid score specified'
};

echo "\n" . $return;

// output
invalid score specified

Be careful of such cases when you have data fetched from DB and you may not sure about about the datatype of data and this may lead to wrong results, for such cases the switch statement is better.

  • match expression must be exhaustive. Meaning that it should be a suitable match exist, otherwise an unhandledMatchError will be thrown:
$score = 20;
$return = match($score) {
    10 => 'You acheived high score',
    5 => 'You acheived medium score'
};

In this example as there is no match arm found this error triggered:

Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type ....

 

Multiple Conditions In Match

The match expression can contain multiple expressions separated by comma, this is if you have multiple values that evaluate to the same result.

Example: If we have some match arms that have same result like so:

$country = 'uk';

$continent = match($country) {
    'uk' => 'europe',
    'france' => 'europe',
    'spain' => 'europe',
    'japan' => 'asia'
};

Which is equivalent to these:

$continent = match($country) {
    'uk', 'france', 'spain' => 'europe',
    'japan' => 'asia'
};

 

default keyword in match

The match expression also contain the default keyword which get’s evaluated if there is no suitable match found:

$country = 'undefined country';

$continent = match($country) {
    'uk', 'france', 'spain' => 'europe',
    'japan' => 'asia',
    default => 'no continent'
};

 

Logical operations in match branches

The match expression not limited to just compare scalar value, but it may contain logical operations as in this example:

$experience = 12;

$return = match (true) {
    $experience >= 15 => 'Team lead',
    $experience >= 10 => 'Senior',
    $experience >= 4 => 'Junior',
    $experience >= 2 => 'Beginner'
};

$return;

// Senior

 

0 0 votes
Article Rating

What's your reaction?

Excited
0
Happy
1
Not Sure
0
Confused
0

You may also like

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments