PHP match expression tutorial shows how to do flow control in PHP with match. A match expression appeared first in PHP 8.
last modified February 16, 2025
PHP match expression tutorial shows how to do flow control in PHP with match.
A match expression is a powerful control flow construct, in which we compare values against multiple alternatives. It is similar to the switch expression.
A match expression returns a value. It must be terminated with a semicolon. A match arm compares values strictly (===). Match arms do not fall-through to later cases. The default keyword is used for all other options not included in the arms.
In if/else or switch statements, each individual condition is called a branch; in pattern matching, the term arm is used instead.
In the first example, we match against integer literals.
main.php
<?php
$menu = <<<MENU Select option 1 - start 2 - slow down 3 - accelerate 4 - pause 5 - terminate MENU;
echo “$menu\n”;
$opt = intval(trim(fgets(STDIN)));
$res = match ($opt) {
1 => 'start',
2 => 'slow down',
3 => 'accelerate',
4 => 'pause',
5 => 'terminate',
default => 'unknown'
};
echo “your option: $res\n”;
We have a menu of options; each option is represented by an integer.
$opt = intval(trim(fgets(STDIN)));
We read a value from a command line.
$res = match ($opt) {
1 => 'start',
2 => 'slow down',
3 => 'accelerate',
4 => 'pause',
5 => 'terminate',
default => 'unknown'
};
The match expression matches the selected option against a list of values. Each arm is separated by comma. For all other options, we have the default keyword.
echo “your option: $res\n”;
We print the chosen option.
In the second example, we match against string literals.
main.php
<?php
$langs = [‘russian’, ‘slovak’, ‘german’, ‘swedish’, ‘hungarian’, ‘french’, ‘spanish’];
echo(“say hello\n”);
foreach ($langs as $lang) {
$res = match ($lang) {
'russian' => 'привет',
'hungarian' => 'szia',
'french' => 'salut',
'spanish' => 'hola',
'slovak' => 'ahoj',
'german' => 'hallo',
'swedish' => 'Hallå'
};
echo "$res\n";
}
We have a list of languages. We go through the list and say hello for each language.
$ php main.php say hello привет ahoj hallo Hallå szia salut hola
Multiple options in one arm are separated with a comma.
main.php
<?php
$grades = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘FX’];
foreach ($grades as $grade) {
$res = match ($grade) {
'A' , 'B' , 'C' , 'D' , 'E' , 'F' => 'passed',
'FX' => 'failed'
};
echo "$res\n";
}
We have a list of grades. For A throug F grades, we pass the example. For the FX grade, we fail the exam.
$ php main.php passed passed passed passed passed passed failed
In the next example, we have conditions in the match arms.
main.php
<?php
$r = rand(-5, 5);
$res = match (true) { $r < 0 => “${r}: negative value”, $r === 0 => “${r}: zero”, $r > 0 => “${r}: positive value” };
echo “$res\n”;
The example chooses a random integer. With match we determine, if the value is negative, zero, or positive.
$r < 0 => “${r}: negative value”,
This arm is executed if the $r is less than zero.
In this article we have presented the PHP match expression.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP tutorials.