Lesson 13 of 50 – PHP Match Expression
26%

PHP Match Expression

The match expression was introduced in PHP 8.0. It compares a value against several possible conditions and returns the result associated with the first matching condition. It is an alternative to many simple switch statements.

1. What Is the Match Expression?

A match expression compares a subject with different values using strict comparison. It returns the result of the matching arm.

Syntax:

$result = match ($value) {
    value1 => result1,
    value2 => result2,
    default => defaultResult,
};

Example:

<?php
$day = 3;

$result = match ($day) {
    1 => "Monday",
    2 => "Tuesday",
    3 => "Wednesday",
    4 => "Thursday",
    5 => "Friday",
    default => "Weekend or invalid day",
};

echo $result;
?>

Output:

Wednesday

Since $day is 3, the expression returns "Wednesday".

2. Match Uses Strict Comparison

Unlike a traditional PHP switch, match compares values using strict comparison, similar to ===. Both value and type must match.

<?php
$value = 10;

$result = match ($value) {
    10 => "Integer ten",
    "10" => "String ten",
    default => "No match",
};

echo $result;
?>

Output:

Integer ten

The integer 10 matches the integer arm. The string "10" is a different type and does not match the integer value.

3. Using the default Arm

The default arm handles values that do not match any other arm.

<?php
$color = "Purple";

$result = match ($color) {
    "Red" => "Color is Red",
    "Blue" => "Color is Blue",
    "Green" => "Color is Green",
    default => "Color not found",
};

echo $result;
?>

Output:

Color not found

If no arm matches and no default arm exists, PHP throws an UnhandledMatchError. Include default when unmatched values are possible and should be handled.

4. Grouping Multiple Conditions

You can separate multiple values with commas when they should produce the same result.

<?php
$day = "Sunday";

$result = match ($day) {
    "Saturday", "Sunday" => "Weekend",
    "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday" => "Working day",
    default => "Invalid day",
};

echo $result;
?>

Output:

Weekend

Both Saturday and Sunday return the same result. This avoids repeating the same expression.

5. Match with a Grade Calculator

A match expression can select a grade message based on a grade value.

<?php
$grade = "B";

$result = match ($grade) {
    "A" => "Excellent",
    "B" => "Very Good",
    "C" => "Good",
    "D" => "Needs Improvement",
    "F" => "Fail",
    default => "Invalid grade",
};

echo $result;
?>

Output:

Very Good

Notice that match returns a value. You can store that value in a variable or use it directly in another expression.

6. Match with Calculations

The right-hand side of each arm can contain an expression, including a calculation.

<?php
$a = 20;
$b = 5;
$operator = "*";

$result = match ($operator) {
    "+" => $a + $b,
    "-" => $a - $b,
    "*" => $a * $b,
    "/" => $b != 0
        ? $a / $b
        : "Cannot divide by zero",
    default => "Invalid operator",
};

echo $result;
?>

Output:

100

The selected arm calculates the multiplication result. The division arm also checks for division by zero before calculating.

7. Returning Boolean Values

A match expression can return Boolean values or other data types.

<?php
$status = "active";

$isActive = match ($status) {
    "active" => true,
    "inactive" => false,
    default => null,
};

var_dump($isActive);
?>

Output:

bool(true)

The result can be used in conditions or passed to other functions.

8. Match as a Function Argument

You can use a match expression directly as a function argument instead of assigning it to a separate variable first.

<?php
$month = 4;

echo match ($month) {
    1 => "January",
    2 => "February",
    3 => "March",
    4 => "April",
    5 => "May",
    6 => "June",
    default => "Other month",
};
?>

Output:

April

PHP evaluates the match expression first and passes the resulting string to echo.

9. Match vs. Switch
Match Switch
Introduced in PHP 8.0. Available in older PHP versions.
Returns a value. Executes statements.
Uses strict comparison. Uses loose comparison.
Does not fall through to later arms. Can fall through if break is omitted.
Uses commas to group values. Uses multiple case labels to group values.
Requires a matching arm or default. Can finish without a matching case.

Example comparison:

<?php
$value = 5;

$message = match ($value) {
    1 => "One",
    5 => "Five",
    default => "Other",
};

echo $message;
?>

Output:

Five

Use match when you want to map values to results. Use if-else for ranges and complex Boolean conditions.

📝 Practical Assignment

Write PHP programs to complete these exercises:

  1. Display a day name using a number from 1 to 7.
  2. Display a month name using a number from 1 to 12.
  3. Create a calculator using +, -, * and /.
  4. Display a message for grades A, B, C, D and F.
  5. Group Saturday and Sunday into the same Weekend result.
  6. Return true when a status is active and false when inactive.
  7. Use default to handle an invalid menu choice.
  8. Test the difference between integer 10 and string "10".

Sample starting code:

<?php
$choice = 2;

$result = match ($choice) {
    1 => "Home",
    2 => "About",
    3 => "Contact",
    default => "Invalid choice",
};

echo $result;
?>

Expected output:

About

🧠 Quick Quiz

1. In which PHP version was match introduced?




2. Which comparison rule does match use?




3. What happens if no arm matches and there is no default arm?