You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spec.md
+24-7Lines changed: 24 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1088,13 +1088,18 @@ if (
1088
1088
1089
1089
### 5.2 `switch`, `case`, `match`
1090
1090
1091
-
A `switch` structure looks like the following. Note the placement of
1092
-
parentheses, spaces, and braces. The `case` statement MUST be indented once
1093
-
from `switch`, and the `break` keyword (or other terminating keywords) MUST be
1094
-
indented at the same level as the `case` body. There MUST be a comment such as
1095
-
`// no break` when fall-through is intentional in a non-empty `case` body.
1096
-
The `case` and `default` keywords MUST use colons as shown in the sample code below.
1091
+
A switch structure must follow the rules below:
1097
1092
1093
+
*`case` statements MUST be indented one level from the `switch`.
1094
+
* The `case` statements line MUST consist only of the `case` keyword, a single space, the case condition (an expression), and a colon.
1095
+
* If the case condition is sufficiently complex to warrant being multi-line, it MUST be wrapped in parentheses, MUST have the opening parenthesis on the same line as the `case` keyword, and MUST end with a line containing only the closing parenthesis and colon, with no space between them.
1096
+
* The body of a `case` statement MUST be indented one level from the `case`.
1097
+
* If a non-empty case intends to continue into the following case, then a clear comment MUST be included to highlight the deliberate lack of a `break`, `return`, or similar termination statement. Examples include "No break," "Deliberate fall-through," etc.
1098
+
* All other non-empty cases MUST have a terminating `break`, `return`, or similar termination statement, even if they are the final one in the `switch` block.
1099
+
* The `case` body MUST NOT be wrapped in `{}`.
1100
+
* The default statement MUST be indented one level from the `switch`, and the default keyword MUST be followed by a colon.
1101
+
1102
+
See the example below.
1098
1103
1099
1104
```php
1100
1105
<?php
@@ -1105,7 +1110,7 @@ switch ($expr) {
1105
1110
break;
1106
1111
case 1:
1107
1112
echo 'Second case, which falls through';
1108
-
// no break
1113
+
// No break
1109
1114
case 2:
1110
1115
case 3:
1111
1116
case 4:
@@ -1134,6 +1139,18 @@ switch (
1134
1139
}
1135
1140
```
1136
1141
1142
+
```php
1143
+
<?php
1144
+
1145
+
switch (true) {
1146
+
case (
1147
+
$a === 10
1148
+
&& $b === 20
1149
+
):
1150
+
break;
1151
+
}
1152
+
```
1153
+
1137
1154
Similarly, a `match` expression looks like the following. Note the placement
0 commit comments