Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The match expression is a control structure introduced in PHP 8.0 that evaluates a subject value against multiple conditions and returns the value of the first matching expression. Unlike the traditional switch statement, match is an expression that evaluates to a value, enforces strict type comparison, and does not exhibit implicit fallthrough behavior.
<?php
$statusCode = 200;

$message = match ($statusCode) {
    200, 201 => 'Request successful',
    400 => 'Bad request',
    404 => 'Not found',
    default => 'Unknown status',
};

echo $message; // Outputs: Request successful

Core Mechanics

  • Expression Evaluation: Because match is an expression, it evaluates to a single value. This result can be directly assigned to a variable, returned from a function, or passed as an argument.
  • Strict Comparison: The match expression compares the subject to the conditions using strict identity (===). Both the value and the data type must match exactly. A string "1" will not match an integer 1.
  • No Fallthrough: Execution strictly terminates after the first matching arm is evaluated. There is no implicit fallthrough to subsequent conditions, eliminating the need for break statements.
  • Comma-Separated Conditions: A single match arm can evaluate multiple conditions separated by commas. If any condition in the list strictly matches the subject, the corresponding right-hand expression is evaluated and returned.
  • Exhaustiveness: The match expression must be exhaustive. If the subject does not match any condition and no default arm is provided, PHP will throw an UnhandledMatchError at runtime.
  • Single Expression Arms: The right side of a match arm (=>) must be a single expression. It cannot be a multi-line block of statements.

Execution Behavior

When the PHP engine encounters a match expression, it executes using the following sequence:
  1. The subject expression is evaluated once.
  2. The engine iterates through the match arms sequentially from top to bottom.
  3. For each arm, the condition(s) on the left side of the => operator are evaluated.
  4. If a strict match (===) is found, the single expression on the right side of the => is evaluated, and its result is returned as the value of the entire match block.
  5. If no match is found in the specific conditions, the default arm is evaluated and returned.
  6. If no match is found and no default arm exists, an exception is thrown.

Strict Type Comparison Visualization

<?php
$subject = '2';

$result = match ($subject) {
    1, 2 => 'Integer match',       // Fails: Strict comparison ('2' !== 2)
    '1' => 'String one',           // Fails: '2' !== '1'
    '2' => 'String two',           // Succeeds: '2' === '2'
    default => 'Fallback value',   // Ignored due to prior match
};

echo $result; // Outputs: String two

Advanced Evaluation (True Subject)

The match expression can also be used with true as the subject to evaluate arbitrary conditional expressions, functioning similarly to a chained if-elseif structure but returning a value. In this pattern, the engine evaluates each condition until one resolves to boolean true.
<?php
$value = 75;

$result = match (true) {
    $value > 100 => 'High',
    $value > 50 => 'Medium',
    $value > 0 => 'Low',
    default => 'Zero or Negative',
};

echo $result; // Outputs: Medium
Master PHP with Deep Grasping Methodology!Learn More