How can I break an outer loop with PHP? How can I break an outer loop with PHP? php php

How can I break an outer loop with PHP?


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;


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 gotos, 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)