How can I break an outer loop with PHP?
Solution 1:
In the case of 2 nested loops:
break 2;
http://php.net/manual/en/control-structures.break.php
Solution 2:
PHP Manual says
break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.
break 2;
Solution 3:
You can using just a break-n statement:
foreach(...)
{
foreach(...)
{
if (i.name == j)
break 2; //Breaks 2 levels, so breaks outermost foreach
}
}
If you're in php >= 5.3, you can use labels and goto
s, similar as in ActionScript:
foreach (...)
{
foreach (...)
{
if (i.name == j)
goto top;
}
}
top:
But goto
must be used carefully. Goto is evil (considered bad practice)