Match Expressions in PHP8 over Switch Statement

altsyset

Alula TYC

Posted on November 5, 2021

Match Expressions in PHP8 over Switch Statement

Match expressions in PHP8 are among the many new features that encourage good standards when writing code. At first, they might seem just another short-hand syntax. However, match expressions effectively replace switch statements and make our code cleaner, readable, and less prone to errors.

How?

Let’s see match expressions in action. And you would be convinced to refactor your code.

Match Expression Syntax

First, let’s observe the general syntax of match expressions. Similar to associative arrays, the general syntax of match expressions has a key and value pair. Take a look.

$expression = strpos("PHP is great", "PHP");

$resultOfExp = match($expression) {
    0 => "Sentence starts with PHP",
    9 => "Sentence ends with PHP",
   'default' => "Sentence doesn't start or end with PHP"
}
Enter fullscreen mode Exit fullscreen mode

So match is a method that accepts an expression that computes to different values. Note that, $expression could be anything, a function call or comparison operator. In our example above, we have made a simple function call to strpos which returns the index of the second string in the first string.

Hence, if it computes to 0 then our variable $resultOfExp will get "Sentence starts with PHP". Or if it computes to 9 then our variable gets the “Sentence ends with PHP” string value. Finally, if the result of the expression is neither 0 nor 9, our variable will be assigned a value of “sentence doesn’t start or end with PHP”.
You might be wondering that this is just like a switch statement or an if-else statement. And you are right. It is just a cleaner and less error-prone switch statement.
Now let’s see the same code in a switch statement.

switch($expression) {
   case 0:
     $resultOfExp = "Sentence starts with PHP";
     break;
   case 9:
     $resultOfExp = "Sentence ends with PHP";
     break;
   default:
     $resultOfExp = "Sentence doesn't start or end with PHP"
}
Enter fullscreen mode Exit fullscreen mode

As you can see, our switch statement is much more verbose with multiple variable assignments to $resultOfExp. As a result, we are prone to errors and our code could easily end up unreadable.

Difference between switch and match expressions in PHP8

Besides these advantages match expressions have the following advantages over a switch statement.

Switch Statement Match Expressions
Loose type comparison. For example ($a == $b) Strict type comparison. For example ($a === $b)
Doesn’t return a value. Hence we have to assign the result over and over again. The result of the expression is returned. Therefore we assign to our variable only once.
Each case has multiple commands or multiple lines of code. Only one expression per case.
Finally, a switch statement requires an unnecessary and verbose break statement. Whereas, match expressions do not need any additional verbose statement.

To sum up, match expressions are just like switch statements which are clearly intended to make our code less error-prone and readable. And I hope this article convinced you to consider using match expressions over switch statements in the future.

💖 💪 🙅 🚩
altsyset
Alula TYC

Posted on November 5, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related