docs: English JsonRpcBundle docs, REST and MCP Bundle sections, package cards
en/articles/from-tg/php/match-vs-switch.md
new file +126
β0
1
+
# match !== switch
2
+
3
+
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`.
4
+
5
+
But that's not true.
6
+
7
+
Let me try to explain why `match` is a separate tool with its own advantages and purpose.
8
+
9
+
## 1. match returns a value
10
+
11
+
`match` is an expression, not a statement. It always returns a result that you assign to a variable:
12
+
13
+
```php
14
+
$taxRate = match($countryCode) {
15
+
'UA' => 0.20,
16
+
'PL' => 0.23,
17
+
'DE' => 0.19,
18
+
default => 0.0,
19
+
};
20
+
```
21
+
22
+
`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.
23
+
24
+
## 2. All cases must be covered
25
+
26
+
`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.
27
+
28
+
```php
29
+
$action = match($input) {
30
+
'start' => 'Start',
31
+
'stop' => 'Stop',
32
+
default => 'Pause',
33
+
};
34
+
```
35
+
36
+
In `switch` and `if/else`, any missing case is simply ignored.
37
+
38
+
## 3. Always strict comparison (===), not loose (==)
39
+
40
+
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:
41
+
42
+
```php
43
+
match(0) {
44
+
false => 'false', // β UnhandledMatchError
45
+
0 => 'zero', // β
will be executed
46
+
};
47
+
48
+
switch (0) {
49
+
case false:
50
+
echo 'false'; // β
will be printed
51
+
break;
52
+
case 0:
53
+
echo 'zero'; // β never reached
54
+
break;
55
+
}
56
+
```
57
+
58
+
In this case `switch` picks `false` as a match for `0`, which can lead to unexpected behavior.
59
+
60
+
## 4. No fallthrough and no break
61
+
62
+
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.
63
+
64
+
If you don't know what I'm talking about, just run this code:
65
+
66
+
```php
67
+
$value = 2;
68
+
switch ($value) {
69
+
case 1:
70
+
echo 'one';
71
+
case 2:
72
+
echo 'two';
73
+
case 3:
74
+
echo 'three';
75
+
}
76
+
```
77
+
78
+
## 5. It nurtures a declarative style
79
+
80
+
`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:
81
+
82
+
```php
83
+
return match(true) {
84
+
$x > 0 => 'positive',
85
+
$x < 0 => 'negative',
86
+
default => 'zero',
87
+
};
88
+
```
89
+
90
+
## 6. Performance: O(1) lookup by value
91
+
92
+
`match` works like a hash map: for scalar values (`int`, `float`, `string`, `bool`) it looks up in constant time β [O(1) algorithmic complexity](/articles/from-tg/series/algorithm-complexity/o-1). This gives it a big advantage over `switch`, where each case is compared sequentially ([O(n) algorithmic complexity](/articles/from-tg/series/algorithm-complexity/o-n)):
93
+
94
+
```php
95
+
// match β O(1):
96
+
$color = match($code) {
97
+
'R' => 'Red',
98
+
'G' => 'Green',
99
+
'B' => 'Blue',
100
+
default => 'Unknown',
101
+
};
102
+
103
+
// switch β O(n):
104
+
switch ($code) {
105
+
case 'R': $color = 'Red'; break;
106
+
case 'G': $color = 'Green'; break;
107
+
case 'B': $color = 'Blue'; break;
108
+
default: $color = 'Unknown';
109
+
}
110
+
```
111
+
112
+
## Conclusions
113
+
114
+
> `match` is not an alternative to `switch` or `if/else`, but a separate tool for situations where clarity and strictness matter, that is, for all situations with conditions.
115
+
>
116
+
> It helps avoid the classic `switch` mistakes and encourages cleaner, more predictable code.
117
+
>
118
+
> It's fast: simple values are looked up in constant time.
119
+
120
+
If you don't use `match` in your daily work yet, now is the time to try it in your next task.
121
+
122
+
As they say: "Feel the difference" π
123
+
124
+
---
125
+
126
+
*First published in Ukrainian in the Telegram channel [βCreated to dieβ](https://t.me/php_while_true/27) on June 21, 2025.*