[thelist] PHP if exit Q

Kelly Hallman khallman at wrack.org
Tue Aug 26 16:05:51 CDT 2003

On Tue, 26 Aug 2003, Jay Blanchard wrote:
> [snip] If I am in a second level IF conditional and it fails, I want to
> jump out of the parent IF. How can I do that? [/snip]
> break;

break does not escape out of an if conditional-- only for, foreach,
while, do..while or switch control structures.  Try encapsulating in a 
one-shot do..while and you'll be able to use break:

do {
    if ($cond1) {
        if (!$cond2) { break; }
} while(0);

do { } while(0); will always run only once, since the condition is
evaluated at the end of the loop and is always false.  So, it
is a clean way to break out of a block of code at any point.

Also note that you may be able to use this to avoid nested if's:

do {
    if ($cond1) { ... } else { break; }
    if ($cond2) { ... } else { break; }
} while(0);

Kelly Hallman

More information about the thelist mailing list