Maaz Zindani

Loading the index…

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

PHP stops with 'break' not in the 'loop' or 'switch' context

ErrorFatal error: 'break' not in the 'loop' or 'switch' context in

Short answer

PHP found a break or continue that no loop or switch encloses. Since PHP 7.0 this is detected when the file is compiled, so the whole file fails before anything runs. Open the file and line named in the error, then either move the statement inside a loop or switch or remove it, and give break a literal number if it has an argument.

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

Fix it fast

  1. 1Find out which cause you have

    The message names the file and the line. Print the area around that line, then read upward until you find the structure that should enclose the break. If you meet the start of a function or the top of the file first, no loop encloses it.

    php -l path/to/file.php
    sed -n '30,60p' path/to/file.php
  2. 2Match what you see, then jump to its fix

    1. 1The line sits in an if block, a function body or the top level of a template, and no for, foreach, while, do-while or switch opens above it in the same fileA break or continue is not inside any loop or switchFix
    2. 2The line reads break SOME_CONSTANT; or continue $levels;break or continue is given a constant instead of a numberFix

What does the error look like?

  • Fatal error: 'break' not in the 'loop' or 'switch' context in
  • Fatal error: 'break' not in the 'loop' or 'switch' context in /path/to/file.php on line 42

Cause 1 of 2

A break or continue is not inside any loop or switch

1Check

The PHP manual says break ends execution of the current for, foreach, while, do-while or switch structure. PHP 7.0 changed break and continue outside such a structure from a run-time problem to a compile-time E_COMPILE_ERROR, so an old file that ran on PHP 5 can now fail on load. A loop in a different function or a different file does not enclose the statement.

grep -n "break\|continue" path/to/file.php

2Fix

Put the statement back inside the loop or switch it was meant for, or remove it. If a function was meant to stop the code that called it, return from the function and let the loop check the result.

<?php
foreach ($items as $item) {
    if ($item === $wanted) {
        $found = $item;
        break; // fine: a foreach encloses this break
    }
}

3Verify

Check the file again, then load the page.

php -l path/to/file.php

Confirmed by php.net

Did this fix it?

Cause 2 of 2

break or continue is given a constant instead of a number

1Check

The PHP 7.0 migration guide says break and continue no longer allow their argument to be a constant, and that this triggers an E_COMPILE_ERROR. The break manual describes the argument as an optional numeric argument that says how many nested enclosing structures to leave.

grep -nE "(break|continue) +[A-Za-z_$]" path/to/file.php

2Fix

Write the number as a literal in the code. The default is 1, which leaves only the immediate enclosing structure.

<?php
foreach ($rows as $row) {
    foreach ($row as $cell) {
        if ($cell === null) {
            break 2; // leave both loops
        }
    }
}

3Verify

Check the file again for compile errors.

php -l path/to/file.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Run php -l on every changed file in a pre-commit hook or in CI, so a compile error never reaches the server.
  • After you delete or move a loop, search the block that used to sit inside it for break and continue.
  • Write the level of a nested break as a plain number, and keep nesting shallow enough that the number is obvious.
  • Test on the PHP version you deploy, because code that loaded on PHP 5 can fail on load on PHP 7 and later.

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.