Cannot Use Non-Constant Argument With Continue or Break
As of PHP 5.4.0, it is no longer possible to use a non-constant argument with continue
or break
. For example, an arithmetic operation in this context will result in a fatal error:
while (true) {
break 0 + 1;
}
See execution result.
Other discontinued argument types:
- Zero:
break 0
- Numeric string:
break '1'
- Variable:
break $num
- Function call:
break min(1, 2)
- Type cast:
break (int) 1
As of PHP 7.0.0, it is also no longer possible to use a constant:
while (true) {
break E_ERROR;
}
Solution
Since you need to know the argument at runtime, you need to analyze the logic of your code and use the appropriate positive integer.
Errors or Warnings
- 'break' operator with non-integer operand is no longer supported
- 'break' operator with non-constant operand is no longer supported