PHP stops with 'break' not in the 'loop' or 'switch' context
Fatal error: 'break' not in the 'loop' or 'switch' context inShort 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.
Fix it fast
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.php2Match what you see, then jump to its fix
- 1
The 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
The line reads break SOME_CONSTANT; or continue $levels;break or continue is given a constant instead of a numberFix
- 1
What does the error look like?
Fatal error: 'break' not in the 'loop' or 'switch' context inFatal 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.php2Fix
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.phpConfirmed by php.net
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.php2Fix
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.phpConfirmed by php.net
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
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