Cannot Use Continue and Break Statements Outside Loop or Switch
Prior to PHP 7.0.0, a continue
or break
outside of a loop or switch control structure would result in a fatal error at runtime. As of PHP 7.0.0, the error happens at compile-time:
if (false) {
continue;
}
See execution result.
Solution
The execution of the invalid code would cause an error regardless of the PHP version. It's therefore safe to assume that the problematic code was either never called, or was always resulting in a fatal error. If we can confirm that the problematic code is indeed unused, we can remove it:
- if (false) {
- continue;
- }
Errors or Warnings
- 'continue' not in the 'loop' or 'switch' context