As of PHP 7.0, instead of code executing up until encountering a continue (or break) call outside of a loop statement, the code will simply not execute.
If you need to correct such error cases as part of an upgrade, you may need to substitute either an exit or return to maintain the existing behavior of such legacy code.
<?php
class ok {
function foo() {
echo "start\n";
for ($i = 0; $i < 5; $i++) {
echo "before\n";
$this->bar($i);
echo "after\n";
}
echo "finish\n";
}
function bar($i) {
echo "inside iteration $i\n";
if ($i == 3) {
echo "continuing\n";
continue;
}
echo "inside after $i\n";
}
}
$ex = new ok();
$ex->foo();
?>
sh> php56 continue.php
start
before
inside iteration 0
inside after 0
after
before
inside iteration 1
inside after 1
after
before
inside iteration 2
inside after 2
after
before
inside iteration 3
continuing
PHP Fatal error: Cannot break/continue 1 level in continue.php on line 22
PHP Stack trace:
PHP 1. {main}() continue.php:0
PHP 2. ok->foo() continue.php:31
PHP 3. ok->bar() continue.php:10
sh> php70 continue.php
PHP Fatal error: 'continue' not in the 'loop' or 'switch' context in continue.php on line 22
Fatal error: 'continue' not in the 'loop' or 'switch' context in continue.php on line 22