Break out of if/else?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Break out of if/else?

Post 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}
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Break out of if/else?

Post 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.
Last edited by Zoxive on Tue Mar 18, 2008 3:41 pm, edited 1 time in total.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Break out of if/else?

Post by alex.barylski »

You need to break out of an IF -- have you tried break -- out of curiosity?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Break out of if/else?

Post 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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Break out of if/else?

Post 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?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Break out of if/else?

Post 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.
Post Reply