Please wait

Match Expression

The match expression is a new feature in PHP 8.0 that provides an alternative to switch statements for pattern matching and executing code based on the results of the match. It's very similar to the switch statement, except the match keyword is used as an expression.

What is an expression?

An expression in PHP is a combination of values, variables, operators, and functions that can be evaluated to a single value. An expression can be a single value, such as a string or number, or it can be a more complex combination of values, variables, and operators that evaluate a single result.

Examples of simple expressions in PHP include:

  • 5
  • "Hello World"
  • true
  • $num + 1

Complex expressions can be formed by combining multiple values, variables, and operators, such as:

  • $num1 + $num2
  • "{$name} is {$age} years old"
  • $num > 10 ? "Greater than 10" : "Less than or equal to 10"

Expressions are an essential part of PHP and are used in many different contexts, such as:

  • Assigning values to variables
  • Performing calculations
  • Evaluating conditions in control structures like if statements
  • Forming arguments for function calls.

Syntax

The basic syntax for a match expression is as follows:

match (expression) {
  pattern1 => return_expression1,
  pattern2 => return_expression2,
  default => return_expression3
};

In this example, expression is the value to be matched, pattern1 and pattern2 are patterns to match against, and return_expression1 and return_expression2 are the corresponding expression to return if the match is successful. The default case provides a fallback if none of the patterns match.

Using a match expression

Here's a simple example that uses the match expression to output the month of the year based on a number:

$monthNum = 3;
 
$monthName = match ($month) {
  1 => "January",
  2 => "February",
  3 => "March",
  4 => "April",
  5 => "May",
  6 => "June",
  7 => "July",
  8 => "August",
  9 => "September",
  10 => "October",
  11 => "November",
  12 => "December",
  default => "Unknown month"
};
 
echo $monthName;

In this example, we're using the match keyword to perform a match expression against a number. Inside the expression, PHP will compare the value against various patterns. If it finds a match, the value written to the right of the arrow is returned. This value is stored as the value for the $monthName variable.

Note that match expressions must return a value and can be used in an expression context. This makes them a powerful and concise alternative to switch statements, especially when matching against patterns and executing code based on the results of the match.

Multiple patterns

We're not limited to a single pattern. Multiple patterns can be specified by separating them with a , character. Take the following:

$month = 7;
 
$season = match ($month) {
    1, 2, 12 => "Winter",
    3, 4, 5 => "Spring",
    6, 7, 8 => "Summer",
    9, 10, 11 => "Autumn",
    default => "invalid",
};
 
echo "The season is $season.";

In this example, we're running the $month variable against various patterns. If the value matches a pattern, the corresponding season name is assigned to the $season variable. If no match is found, the default case is executed, and an "invalid" message is displayed.

Exercise

Create a script that uses the match expression to determine the letter grade for a student based on their number grade. The following table should be used for determining the letter grade:

  • 5: A
  • 4: B
  • 3: C
  • 2: D
  • 1: F

Key takeaways

  • The match expression is a feature in PHP for matching values against patterns.
  • A list of patterns can be written inside a match expression separated by commas.
  • Multiple patterns can be provided by separating them with commas.

Comments

Please read this before commenting