docs: English JsonRpcBundle docs, REST and MCP Bundle sections, package cards
en/articles/from-tg/php/match-vs-switch.md
new file +126
−0
match !== switch
A new tool appeared quite a while ago — starting with PHP 8.0: match. But interview practice (with both juniors and middles) shows that many people, at best, just know about it but don't use it in everyday code. The main reason is a superficial understanding: there is an opinion that it's just more syntactic sugar over switch or if/else.
But that's not true.
Let me try to explain why match is a separate tool with its own advantages and purpose.
1. match returns a value
match is an expression, not a statement. It always returns a result that you assign to a variable:
$taxRate = match($countryCode) {
'UA' => 0.20,
'PL' => 0.23,
'DE' => 0.19,
default => 0.0,
};
switch and if/else, by contrast, return nothing. If you want a result, you have to declare a variable before the condition and then set its value in each case.
2. All cases must be covered
match forces the developer to handle every possible option or to specify default. If there's no match, you get an UnhandledMatchError. This significantly reduces the risk of non-obvious bugs.
$action = match($input) {
'start' => 'Start',
'stop' => 'Stop',
default => 'Pause',
};
In switch and if/else, any missing case is simply ignored.
3. Always strict comparison (===), not loose (==)
In match, values are compared strictly — without type juggling. It's one more nod from PHP towards strict typing and a plus for safety and predictability:
match(0) {
false => 'false', // ❌ UnhandledMatchError
0 => 'zero', // ✅ will be executed
};
switch (0) {
case false:
echo 'false'; // ✅ will be printed
break;
case 0:
echo 'zero'; // ❌ never reached
break;
}
In this case switch picks false as a match for 0, which can lead to unexpected behavior.
4. No fallthrough and no break
With switch there is always the risk of forgetting a break, and then the code "falls through" into the next case. It's a classic source of bugs. In match every case is a separate rule, and there is no such thing as break or moving on to the next one.
If you don't know what I'm talking about, just run this code:
$value = 2;
switch ($value) {
case 1:
echo 'one';
case 2:
echo 'two';
case 3:
echo 'three';
}
5. It nurtures a declarative style
match encourages a declarative style — without extra logic in the body of the construct. It doesn't let you write long blocks of code in each case, only expressions. This forces you to structure your code better:
return match(true) {
$x > 0 => 'positive',
$x < 0 => 'negative',
default => 'zero',
};
6. Performance: O(1) lookup by value
match works like a hash map: for scalar values (int, float, string, bool) it looks up in constant time — O(1) algorithmic complexity. This gives it a big advantage over switch, where each case is compared sequentially (O(n) algorithmic complexity):
// match — O(1):
$color = match($code) {
'R' => 'Red',
'G' => 'Green',
'B' => 'Blue',
default => 'Unknown',
};
// switch — O(n):
switch ($code) {
case 'R': $color = 'Red'; break;
case 'G': $color = 'Green'; break;
case 'B': $color = 'Blue'; break;
default: $color = 'Unknown';
}
Conclusions
matchis not an alternative toswitchorif/else, but a separate tool for situations where clarity and strictness matter, that is, for all situations with conditions.It helps avoid the classic
switchmistakes and encourages cleaner, more predictable code.It's fast: simple values are looked up in constant time.
If you don't use match in your daily work yet, now is the time to try it in your next task.
As they say: "Feel the difference" 😉
First published in Ukrainian in the Telegram channel “Created to die” on June 21, 2025.