PHP warns that continue inside a switch acts like break
Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"?Short answer
PHP treats switch as a loop for continue, so a plain continue inside a switch only leaves the switch, exactly like break. Since PHP 7.3 that raises a warning because it is usually a mistake. Use continue 2 to go to the next iteration of the enclosing loop, or break if you only meant to leave the switch.
Fix it fast
1Find out which cause you have
The warning names a file and line. Open that line and look at the nearest enclosing structures: is the continue inside a switch that sits inside a foreach, for or while?
php -l path/to/file.php grep -n "continue" path/to/file.php2Match what you see, then jump to its fix
What does the error look like?
Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"?
Cause 1 of 2
A switch inside a loop, and you meant to skip to the next item
1Check
In PHP, switch counts as a looping structure for continue. A bare continue therefore ends the switch only, and the loop body after the switch still runs. The manual says that when a switch is inside a loop, continue 2 goes to the next iteration of the outer loop.
foreach ($rows as $row) {
switch ($row['type']) {
case 'skip':
continue; // warns: this only leaves the switch
}
save($row); // still runs for 'skip' rows
}2Fix
Give continue the level you want. The number says how many enclosing structures to skip to the end of.
foreach ($rows as $row) {
switch ($row['type']) {
case 'skip':
continue 2; // next $row
}
save($row);
}3Verify
Lint the file and run the loop with one row of each type. Skipped rows must not reach save().
php -l path/to/file.phpConfirmed by php.net
Cause 2 of 2
You only wanted to leave the switch
1Check
Older code often used continue as a synonym for break inside case blocks. It behaved like break, so the logic is already correct and only the keyword is misleading.
grep -n -B3 "continue;" path/to/file.php2Fix
Replace continue with break. Behaviour does not change, and the warning goes away.
switch ($mode) {
case 'a':
doA();
break;
}3Verify
Run the file once. The warning no longer appears and the output is the same as before.
php path/to/file.phpConfirmed by php.net
How do I stop it coming back?
- Always write the level you mean: continue 2 for the outer loop, break for the switch.
- Run php -l and your tests under the PHP version you deploy, so version-specific warnings show up in CI.
- Turn warnings into failures in tests (error_reporting set to E_ALL) so they are fixed when they appear.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix