TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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.
Core Mechanics
- Expression Evaluation: Because
matchis 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
matchexpression 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 integer1. - No Fallthrough: Execution strictly terminates after the first matching arm is evaluated. There is no implicit fallthrough to subsequent conditions, eliminating the need for
breakstatements. - 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
matchexpression must be exhaustive. If the subject does not match any condition and nodefaultarm is provided, PHP will throw anUnhandledMatchErrorat 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 amatch expression, it executes using the following sequence:
- The subject expression is evaluated once.
- The engine iterates through the match arms sequentially from top to bottom.
- For each arm, the condition(s) on the left side of the
=>operator are evaluated. - 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 entirematchblock. - If no match is found in the specific conditions, the
defaultarm is evaluated and returned. - If no match is found and no
defaultarm exists, an exception is thrown.
Strict Type Comparison Visualization
Advanced Evaluation (True Subject)
Thematch 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.
Master PHP with Deep Grasping Methodology!Learn More





