The match expression is a modern control structure used to evaluate a single value against multiple choices and return a result.
It was introduced to provide a much stricter, safer, and cleaner alternative to the traditional switch statement.
Unlike a switch statement, match is an expression, meaning it evaluates to a value that you can assign directly to a variable or return from a function. It uses a clean, comma-separated syntax without requiring block keywords or manual exits.
<?php
$result = match ($variable) {
'value1' => 'outcome1',
'value2' => 'outcome2',
default => 'fallback_outcome',
};
Tip for beginners: Because match is an expression being assigned to something, you must place a semicolon ; after its closing curly brace }.
No break required: It automatically terminates as soon as it finds the first match. There is absolutely no risk of "fall-through" bugs.
Strict Comparisons (===): It checks both the value and the exact data type. It will not match a string "20" to an integer 20.
Exhaustiveness: If none of the conditions match, and you do not provide a default arm, PHP will instantly throw an error (UnhandledMatchError) to protect your code from silent bugs.
Here is how you can use a basic match block to dynamically assign a status message:
<?php
$role = "Editor";
$message = match ($role) {
"Admin" => "Welcome to the administrator dashboard.",
"Editor" => "Welcome! You can edit articles.",
default => "Welcome, regular user.",
};
echo $message;
Beyond simplifying basic variable checks, match comes packed with robust capabilities designed to handle complex application logic cleanly.
If you want multiple inputs to trigger the exact same outcome, you can list them on a single line separated by a comma:
<?php
$status = "processing";
$deliveryMessage = match ($status) {
"pending", "processing" => "Your order is being prepared.",
"shipped" => "Your order is on the way!",
"delivered" => "Your order has arrived.",
default => "Unknown order status.",
};
You don't just have to match exact values; you can pass the boolean true into the match parenthesis.
This allows you to evaluate complex mathematical ranges, variable checks, or boolean expressions inside each arm:
<?php
$age = 25;
$ticketType = match (true) {
$age < 12 => "Child Ticket",
$age >= 65 => "Senior Ticket",
default => "Standard Ticket",
};
Each arm of a match expression expects a single expression.
Within an expression, you can even call other functions, or use IIFEs (immediately invoked function expressions) to wrap logic, although it's almost always better to break them out into their own helpers.
<?php
$action = "delete";
$result = match ($action) {
"create" => saveNewRecord(),
"delete" => => (function() {
// this is an immediately invoked function expression
return 'delete ok';
})(),
}();
echo $result; // outputs "delete ok"
How the Logic Flows:
The Strict Check: PHP evaluates the input expression and goes down the arms sequentially, applying strict type and value matching.
Instant Exit: The moment it hits a matching line, it executes the code to the right of the => arrow, returns that value, and completely stops looking further.
Safety Fallback: If nothing matches, it executes the default fallback expression. If no default exists, it alerts the developer with an exception.