Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP warns that continue inside a switch acts like break

ErrorWarning: "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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.php
  2. 2Match what you see, then jump to its fix

    1. 1The warning appears and the switch sits directly inside a foreach, for or whileA switch inside a loop, and you meant to skip to the next itemFix
    2. 2The code after the switch is meant to run for that caseYou only wanted to leave the switchFix

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.php

Confirmed by php.net

Did this fix it?

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.php

2Fix

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.php

Confirmed by php.net

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.