Page 1 of 1

Break out of if/else?

Posted: Tue Mar 18, 2008 3:06 pm
by JAB Creations
I have an if/else argument where 99.9999% of the time the first condition will be met. I attempted to use exit to end the script though it killed the page like die(). Is there a method to stop an if/else without it doing anything?

Code: Select all

if ($condition=="common") {//do nothing but stop this if/else statement?}
else if ($condition=="uncommon1") {//etc1}
else if ($condition=="uncommon2") {//etc2}

Re: Break out of if/else?

Posted: Tue Mar 18, 2008 3:18 pm
by Zoxive
How long is this if/else? And what is its purpose of breaking?

Have you looked at switches?

Before that even...
It seems to me a simple change would remove the whole empty if statement (Where you want to break).

Code: Select all

if($condition != "common"){
  // do my stuff when its not common
}
I think maybe an explanation more of what you want and why would help out some.

Re: Break out of if/else?

Posted: Tue Mar 18, 2008 3:22 pm
by alex.barylski
You need to break out of an IF -- have you tried break -- out of curiosity?

Re: Break out of if/else?

Posted: Tue Mar 18, 2008 3:39 pm
by Zoxive
Hockey wrote:You need to break out of an IF -- have you tried break -- out of curiosity?
PHP Manual - [url=http://www.php.net/break]Break[/url] wrote:break ends execution of the current for, foreach, while, do-while or switch structure.

Code: Select all

Fatal error: Cannot break/continue 1 level

Re: Break out of if/else?

Posted: Tue Mar 18, 2008 4:25 pm
by alex.barylski
I've never had to break from a IF statement -- which makes me question your design, to be honest.

I think PHP 6 is going to include something of a GOTO implementation, but until that time.

Maybe factor the code into a function and call return?

Re: Break out of if/else?

Posted: Tue Mar 18, 2008 4:50 pm
by JAB Creations
Today isn't exactly my sharpest day. :mrgreen: I'll just have it !execute when the most common $condition exists. Oh and I looked up break on php.net being pretty sure it was not part of if/else.