DEV Community

Cover image for How to use β€œmatch” with PHP 8 🎯
Pierre-Henry Soria ✨
Pierre-Henry Soria ✨

Posted on • Updated on

How to use β€œmatch” with PHP 8 🎯

Let's use match expression with PHP 8.0 πŸ’ͺ Lets' see when to use it and why it can have advantages over the old good switch statement.
match will indeed make your code cleaner, less verbose and easier to read πŸ’₯

Furthermore, with the β€œhttps://php.watch/versions/8.0/match-expression” expression, you can have multiple matching conditions separated by commas, like

$sMessage = match ($sDayPrefix) {
  'Sat', 'Sun' => 'Enjoy your weekend!',
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)