Discouraged Continue Targeting Switch
As of PHP 7.3.0, a continue
that is applied to a switch
statement will emit a warning:
switch ('a') {
case 'a':
continue;
}
See execution result.
A continue
inside a switch
statement does not necessarily target that statement. In this example, it targets a while
loop:
while (true) {
switch ('a') {
case 'a':
continue 2;
}
}
Solution
If the switch is not enclosed in a loop, replace continue
with a break
:
switch ('a') {
case 'a':
break;
}
If the switch is enclosed in a loop, add an argument based on the levels that separate it from the loop, including itself:
while (true) {
switch ('a') {
case 'a':
switch ('b') {
case 'b':
continue 3;
}
}
}
Errors or Warnings
- "continue" targeting switch is equivalent to "break"
See Also
- RFC: Deprecate and remove continue targeting switch
"Based on the RFC discussion, we decided to instead implement this as a simple warning"