12. PHP Switch and Match
What you will learn
- How
switchworks, and the need forbreak - The modern
matchexpression - How they differ
- When to use each
The switch statement
switch compares one value against several cases. Each case needs a break, or execution falls through into the next case. default runs when nothing matches.
PHP code
<?php
$day = 3;
switch ($day) {
case 1:
$name = "Monday";
break;
case 2:
$name = "Tuesday";
break;
case 3:
$name = "Wednesday";
break;
default:
$name = "Another day";
}
echo "Day $day is $name";
Output
Day 3 is Wednesday
Grouping cases
Leaving out break on purpose lets several cases share code.
PHP code
<?php
$day = "Sat";
switch ($day) {
case "Sat":
case "Sun":
echo "Weekend";
break;
default:
echo "Weekday";
}
Output
Weekend
A catch with switch
switch compares with loose equality (==), so type conversion can cause surprises. It also needs many lines for simple jobs.
The match expression (PHP 8)
match is a shorter, safer alternative. It returns a value, uses strict comparison (===), needs no break, and throws an error if no arm matches and there is no default.
PHP code
<?php
$day = 3;
$name = match ($day) {
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
default => "Another day",
};
echo "Day $day is $name";
Output
Day 3 is Wednesday
The comma-separated arms mean the same as the long switch, in a quarter of the lines. Remember the semicolon after the closing brace.
Several values in one arm
PHP code
<?php
$day = "Sun";
echo match ($day) {
"Sat", "Sun" => "Weekend",
"Mon", "Tue", "Wed", "Thu", "Fri" => "Weekday",
default => "Unknown",
};
Output
Weekend
match(true) for ranges
To test conditions rather than exact values, match against true.
PHP code
<?php
$score = 72;
$grade = match (true) {
$score >= 90 => "A",
$score >= 70 => "B",
$score >= 50 => "C",
default => "Try again",
};
echo $grade;
Output
B
What if nothing matches?
Without a default, an unmatched match throws an UnhandledMatchError. That is useful: it stops silent mistakes. Add a default when an unknown value is expected.
switch versus match
| switch | match | |
|---|---|---|
| Comparison | Loose (==) |
Strict (===) |
| Returns a value | No | Yes |
Needs break |
Yes | No |
| No match, no default | Does nothing | Throws an error |
| Can run several statements | Yes | One expression per arm |
| PHP version | All | 8.0 and later |
match needs PHP 8. If your host runs an older version, use switch or if. Always run a supported PHP version.
Common mistakes
- Forgetting
breakin aswitch. - Forgetting the semicolon after a
matchblock. - Expecting
matchto convert types."1"does not match1. - Leaving out
defaultwhen unexpected values are possible. - Putting several statements in a
matcharm. Useswitchoriffor that.
Exercise
Use match to turn $code into a message: 200 gives OK, 404 gives Not found, 500 gives Server error, anything else gives Unknown. Test with 404.
Show solution
<?php
$code = 404;
echo match ($code) {
200 => "OK",
404 => "Not found",
500 => "Server error",
default => "Unknown",
};
The output is Not found.
Knowledge check
1. What happens if you forget break in a switch?
Execution falls through into the next case.
2. Which comparison does match use?
Strict comparison (===).
3. What does match do when nothing matches and there is no default?
It throws an UnhandledMatchError.
Summary
switchcompares loosely and needsbreak.match(PHP 8) is shorter, strict, and returns a value.- Use
match(true)for ranges, and includedefaultwhen needed.