Match Expression: How to Use in PHP 8 for Cleaner Code

The match expression, introduced in PHP 8.0, offers a modern way to handle multiple conditional checks.

The new match expression in PHP 8 is a control flow structure that matches a given subject expression with one or more alternative conditional expressions using identity comparison and returns a value from the matched branch.

Match expression syntax is one of the nicest features in PHP 8 that improves the switch syntax in multiple ways.

Key Differences Between Match and Switch

Both switch and match in PHP are used to check conditions and run code. But there are some important differences:

Strict Comparison (===)

  • In switch, PHP uses loose comparison (==).
    Example: switch will treat "5" (string) and 5 (number) as the same.
  • In match, PHP uses strict comparison (===).
    Example: "5" (string) and 5 (number) are not the same.

No Need of break

  • In switch, you must write break after every case, otherwise PHP will continue to the next case.
  • In match, no need to write break. Only the matched condition runs.

Match Can Return a Value Directly

  • switch only executes code, you need extra lines to store a result.
  • match can return values directly and can be used in expressions.

More Compact and Clean

  • switch code is usually longer with multiple cases and breaks.
  • match makes the same logic short and clean.

Example: Switch vs Match

Using switch

<?php
$number = 2;
switch ($number) {
    case 1:
        $result = "One";
        break;
    case 2:
        $result = "Two";
        break;
    default:
        $result = "Other";
}
echo $result; // Output: Two
?>

Using match

<?php
$number = 2;
$result = match ($number) {
    1 => "One",
    2 => "Two",
    default => "Other",
};
echo $result; // Output: Two
?>

PHP 8 Match vs Switch: Quick Comparison

FeatureSwitchMatch
ComparisonUses loose comparison (==)Uses strict comparison (===)
Break RequiredYes, must write break after each caseNo break needed
Return ValueCannot return directly, need extra codeCan return a value directly
Code LengthLonger and repetitiveShort and clean
Multiple MatchesRuns only one case (unless break missing)Can map multiple values to one result
Default CaseOptional, but used for unmatched casesMandatory if all cases are not covered
Error ChancesHigh (if you forget break)Low (no break needed)

Example #1 Structure of a match expression

<?php
$return_value = match (subject_expression) {
    single_conditional_expression => return_expression,
    conditional_expression1, conditional_expression2 => return_expression,
};
?>

Example #2 Basic match usage

<?php
$food = 'cake';

$return_value = match ($food) {
    'apple' => 'This food is an apple',
    'bar' => 'This food is a bar',
    'cake' => 'This food is a cake',
};

var_dump($return_value);
?>

output: string(19) "This food is a cake"

Example #3 Example of using match with comparison operators

<?php
$age = 18;

$output = match (true) {
    $age < 2 => "Baby",
    $age < 13 => "Child",
    $age <= 19 => "Teenager",
    $age >= 40 => "Old adult",
    $age > 19 => "Young adult",
};

var_dump($output);
?>
output: string(8) "Teenager"