2

I was migrating production code from PHP5.3 to PHP7.1, code had break 2; inside nested two level of while loop inside if condition, I got below error:

PHP Fatal error: Cannot 'break' 2 levels

Why two level of break has been removed in PHP7.1 and what could be it's alternate solution?

Ramratan Gupta
  • 1,056
  • 3
  • 17
  • 39

1 Answers1

8

You can solve this issue with change :

break 2;

By:

break;

You are into one loop and not two nested loops. That's why you can't "break 2" (because 2 denotes that you're inside a nested loop). The error appears because PHP7 is stricter than former versions.

Note: You can't "break" from an if statement. You can only break from a loop.

Ave
  • 4,338
  • 4
  • 40
  • 67