Backend Development 4 min read

Using PHP 8 Match Expression to Simplify Conditional Logic

The article introduces PHP 8’s Match expression, explains its syntax, and demonstrates through multiple code examples how it simplifies conditional logic compared to traditional if‑else and switch statements, improving readability and maintainability in backend development.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP 8 Match Expression to Simplify Conditional Logic

PHP 8 introduces a new conditional syntax called the Match expression , which provides a more concise and readable way to evaluate multiple conditions and execute corresponding code blocks, similar to a switch statement but with cleaner syntax.

Match expression syntax :

$result = match ($value) {
    value1 => expression1,
    value2 => expression2,
    value3 => expression3,
    ...
    default => expression
};

In this syntax, $value is the value being tested; when it matches a case, the associated expression is executed and its result is returned.

Consider a simple age‑based classification using traditional if statements:

$age = 20;
if ($age >= 18 && $age <= 25) {
    echo "你是青年人";
} elseif ($age > 25 && $age <= 35) {
    echo "你是中年人";
} elseif ($age > 35) {
    echo "你是老年人";
} else {
    echo "你还是未成年人";
}

The same logic can be expressed more succinctly with a Match expression:

$age = 20;
$result = match (true) {
    $age >= 18 && $age <= 25 => "你是青年人",
    $age > 25 && $age <= 35 => "你是中年人",
    $age > 35 => "你是老年人",
    default => "你还是未成年人"
};
echo $result;

Here the Match expression maps each condition to a result value, and when $value satisfies a condition, the corresponding expression is executed and assigned to $result .

Another example shows how to replace a traditional switch statement that determines a user's permission level based on their role:

$role = "admin";
$permission = "";
switch ($role) {
    case "admin":
        $permission = "完全权限";
        break;
    case "moderator":
        $permission = "部分权限";
        break;
    case "user":
        $permission = "只读权限";
        break;
    default:
        $permission = "未知权限";
}
echo "您的权限级别为:" . $permission;

Using a Match expression, the same logic becomes more compact:

$role = "admin";
$result = match ($role) {
    "admin" => "完全权限",
    "moderator" => "部分权限",
    "user" => "只读权限",
    default => "未知权限"
};
echo "您的权限级别为:" . $result;

These examples demonstrate that the Match expression can replace traditional conditional constructs, making code shorter, easier to read, and more maintainable, which is especially valuable in real‑world backend projects.

Backend DevelopmentConditional StatementsPHP8Match Expression
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.