A website I was visiting failed with
Fatal error: 'break' not in the 'loop' or 'switch' context in
FILE
on lineN
Looking at the source code (found via filename / path - it's an older version of some open source code) showed many functions similar to this:
class Clazz {
// stuff
public static function foo($bar) {
// some code without loops/return
return 'baz';
break; // This is line N
}
}
Now I know that a break
outside of a loop or switch context was allowed in PHP 5 but is no longer since PHP 7, hence the error message.
My questions is: Why would you put a break
after a return anyway? Is/was this some (outdated) idiom?
I've found this pattern in multiple functions in multiple different code bases now; so it doesn't seem like an "left-over" from a refactoring or a misunderstanding of a single developer to me..?