ZetCode

PHP match

last modified August 10, 2022

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.

PHP match integer literals

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.

PHP match string literals

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

PHP match multiple options

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

PHP match conditional arms

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 tutorial, we have presented the PHP match expression.

List all PHP tutorials.