[thelist] PHP if exit Q

Andrew Seguin asegu at borg.darktech.org
Tue Aug 26 18:40:52 CDT 2003


If I understand correctly the intial request... you have some code like
the following:

//some code (0)
if (condition1) {
    //some code (1) here
    if (condition2) {
        //some code (2) here
    } else {
        //skip to "some code (4) here".
    }
    //some code (3) here
}
//some code (4) here.


The problem with break: per http://www.php.net/break :
   "break ends execution of the current for, foreach while, do..while or
switch structure"
so break doesn't work on "if"s. It's only supposed to work on looping
structures, and for the special case of "switch".

instead I'd suggest you take the above code and reshape.. ex:

//some code (0)
if (condition1) {
    //some code (1) here
    if (condition2) {
        //some code (2) here
        //some code (3) here
    } else {
        //--- ignore the idea << skip to "some code (4) here" >>.
        //instead, do nothing; Upon exit of this if,
        //you will automaticaly exit the first level if as well.
    }
}
//some code (4) here.


If you have many levels of ifs...:

if (condition1) {
    if (condition2) {
        if (condition3) {
            //...
            if (condition4) {
            }
        }
    }
}


then how about this instead:


if (condition1) {
    $continue = 1;

    if ($continue && (condition2)) {
    } else {
        $continue = 0;
    }

    if ($continue && (condition3)) {
    } else {
       $continue = 0;
    }

    //...

    if ($continue && (conditionX)) {
    }
}

it's long, but it would save you from nesting a lot of code within if
after if.


Hope these suggestions help,
Andrew

>> -----Original Message-----
>> From: Jay Blanchard [mailto:jay.blanchard at niicommunications.com]
>> Sent: Tuesday, August 26, 2003 2:45 PM
>> To: jsWalter at torres.ws; thelist at lists.evolt.org
>> Subject: RE: [thelist] PHP if exit Q
>>
>>
>> [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;
>
> Yea, tried that first off...
>
>     Fatal error: Cannot break/continue 1 level in
>
> Thanks anyway.
>
> Walter
>
>
> --
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>
>



More information about the thelist mailing list