Azu Posted June 24, 2007 Share Posted June 24, 2007 Hi, can somebody please tell me which command I should use to break out of an if(){blah blah}? In a while(){blah blah} I would use "break;" but that doesn't seem to work in an if(){blah blah}. I know that I could just put all of the rest of the if in {} and use if false so that it will only do the rest of the if if the condition isn't meant, but that is very sloppy and ugly and I really just want to know a function to just break out of the if(){} please.. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted June 24, 2007 Share Posted June 24, 2007 You cant break out of an if. I don't understand what you mean. Post some code for an example of what you mean. Quote Link to comment Share on other sites More sharing options...
.Stealth Posted June 24, 2007 Share Posted June 24, 2007 what do you need to break out of the if for? Quote Link to comment Share on other sites More sharing options...
php_joe Posted June 24, 2007 Share Posted June 24, 2007 the conditions are all in the (). if you require a condition then it looks like this: if($var){blah, blah, blah}. if you require a condition but want to avoid it when another condition applies use if($var1 && !$var2){blah, blah, blah} Quote Link to comment Share on other sites More sharing options...
Azu Posted June 25, 2007 Author Share Posted June 25, 2007 Because I have this big if(){} thing and half-way through it, if a certain condition is met/not met, then it should not run the rest of the if. I was just wandering if there is an elegant way to break out of it, instead of putting the whole rest of the if(){} inside of yet ANOTHER if(){}.. o_o Quote Link to comment Share on other sites More sharing options...
per1os Posted June 25, 2007 Share Posted June 25, 2007 maybe try placing break; inside that if... or putting the if inside a function and just using return; Quote Link to comment Share on other sites More sharing options...
Azu Posted June 29, 2007 Author Share Posted June 29, 2007 Oh right a function. Thanks ^^ that works great. Quote Link to comment Share on other sites More sharing options...
corbin Posted June 29, 2007 Share Posted June 29, 2007 If you mean you want to do something like this (just an example): $i = 0; while($i < 10) { if($i % 2 == 0) { echo "even!"; } else { echo "odd"; } } You could also do it like.... $i = 0; while($i < 10) { if($i % 2 == 0) { echo "even!"; continue; } echo "odd!"; } Quote Link to comment Share on other sites More sharing options...
btherl Posted June 29, 2007 Share Posted June 29, 2007 There's a cute trick I've used to work around php's lack of goto: while (an if condition) { blah blah if (some condition) { break; # goto end } blah if (some other condition) { break; # goto end } blah break; # finish while loop } # This is "end" Dijkstra is rolling in his grave I'm sure.. but seriously, a well considered use of goto can make code clearer. Basically it's an "if" but I turned it into a "while", so I can break out of it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.