2

A website I was visiting failed with

Fatal error: 'break' not in the 'loop' or 'switch' context in FILE on line N

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..?

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
  • That makes no sense to me either. Very strange. – Jonnix Aug 09 '19 at 22:16
  • Are you sure it wasn't something like `if (x) return; break;` where the return was conditional and might be skipped? If not, then as far as I know that code will never make sense. What are some of these code bases? If they are all local to your work it could be the work of one (strange) employee. – Dave S Aug 09 '19 at 22:24
  • @treyBake - no, here the return (supposedly) does not come after a conditional so it always executes. In your link the return might not execute and the question is about whether the return by itself is enough to exit the function. – Dave S Aug 09 '19 at 22:52
  • @DaveS the return wasn't conditional in these functions; code is multiple different open source projects (i add some links to the question asap). Searching for the error message brings up various wordpress plugins for example. – Daniel Jour Aug 09 '19 at 22:57
  • Because it surfaced in multiple code bases I thought it could be some idiom .. or useful trick for testing / debugging – Daniel Jour Aug 09 '19 at 22:58
  • No, it has never been meaningful. Are these public code bases, or just in your organization? It's probably a snippet that was written once (who knows why?) and then copied without noticing the mistake. – Barmar Aug 09 '19 at 23:03
  • Regarding possible refactoring leftovers, maybe those functions used to be cases in a switch? That's the only context where I've really seen people use break after return, even though it's pointless. Just a guess – Don't Panic Aug 09 '19 at 23:10
  • This is redundant code.But Compiler didn't regard it as an error. I think since it was executable for long time, everyone has left it. – Bitsvans Aug 09 '19 at 22:42

1 Answers1

0

Code after return statement will never be executed, then that break is useless.

I would imagine that developers ware testing functions, and they forgot deleting that code, as will never be executed then, there were no problems by that times.

For refactoring that code, just ignore it (delete), cause it had never work.

Vrian7
  • 588
  • 2
  • 6
  • 14