Skip to content

PHP 8: Match Expression

Last updated on November 23, 2022

The new match is similar to the switch and has the following features:

  • The match is an expression, meaning its result can be stored in a variable or returned.
  • Match branches only support single-line expressions and do not need a break; statement.
  • The match does strict comparisons.
<?php
$message = match($var) {
    'a' => 'The variable was a',
    'b' => 'The variable was b',
    'c', 'd' => 'The variable was c or d',
    default => 'The variable was something else',
};

print $message;

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments