PHP 8: Making the Choice Between “switch” and “match”

Doğan Uçar
2 min readAug 31, 2023

--

Photo: Unsplash/@rohit-tandon
Photo: Unsplash/@rohit-tandon

In the constantly changing landscape of PHP, version 8 brought an innovative feature: the match expression. Many see it as a streamlined and more effective counterpart to the traditional switch. Let’s explore their distinctions.

The traditional switch goes something like this:

<?php
$statusCode = 200;
$message = "";
switch ($statusCode) {
case 200:
$message = "OK";
break;
case 500:
$message = "Internal Server Error";
break;
case 404:
$message = "Not Found";
break;
default:
$message = "Unknown Status Code";
break;
}

With PHP 8, the equivalent match looks far more compact:

<?php
$statusCode = 200;

$message = match($statusCode){
200 => "OK",
500 => "Internal Server Error",
404 => "Not Found",
default => "Unknown Status Code"
};

At a glance, the benefits of using match are clear:

  • No need for break statements.
  • Ability to merge different conditions with just a comma.
  • Returns a value directly, making assignments more straightforward.

But that’s just scratching the surface.

Type Strictness: Unlike switch, the match expression is strict with types; akin to using === over ==.

Error Handling: Missing a value without a default in match? PHP will alert you with an UnhandledMatchError.

Simplicity: match offers a clear, singular expression. While fallthrough conditions (multiple lines combining into a single result) as in switch are not directly possible, you can merge conditions in match using commas, achieving similar results with less room for error. If complex logic is needed, make it a function or method that is called.

Performance: match is not just about aesthetics; it’s efficient. Unlike some patterns that evaluate all conditions before providing an outcome, match does it more smartly, ensuring better performance.

Exception Handling: With PHP 8’s throw expressions, you can directly throw exceptions from within a match arm.

Conclusion

While switch has its place, especially when dealing with multiline code blocks, match seems to be its evolved, stricter sibling. Given its strictness, efficiency, and potential future capabilities, match is shaping up to be a worthy contender for many PHP developers. For someone like me who hasn’t used switch recently due to its quirks, match is an exciting addition that might just be the perfect… match.

Need a PHP Developer?

Discover the Power of Expert PHP Development. Elevate your digital projects with precision, innovation, and a commitment to excellence. Partner with a seasoned PHP developer who understands your vision. Let’s shape the future together. Reach out today: https://www.dogan-ucar.de/

originally posted on my personal blog: https://www.dogan-ucar.de/php-8-making-the-choice-between-switch-and-match/

--

--

Doğan Uçar
Doğan Uçar

Written by Doğan Uçar

Software Engineer, PHP/Laminas (Zend), Backend, Cloud, Freelancer & CEO, Open Source Contributor

No responses yet