By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

multiconditional if statement

tolkienarda
100+
P: 316
hi all

in a multi conditional if statement (if a = this and b = that) will the second condition be evaluated if the first statement is false. the reason i am asking is in the real if statement the first condition is to stop the second from being a divide by 0 error;

thanks

eric
Feb 16 '07 #1
Share this Question
Share on Google+
7 Replies


tolkienarda
100+
P: 316
ok turns out i don't need that but if you know off the top or your head i would like to know for future reference.

eric
Feb 16 '07 #2

Motoma
Expert 2.5K+
P: 3,235
PHP uses Short Circuit Evaluation.

[PHP]
if( retTrue() OR retFalse() ) // retFalse() never gets called.
if( retFalse() AND retTrue() ) // retTrue() never gets called.[/PHP]
Feb 16 '07 #3

tolkienarda
100+
P: 316
thanks
eric
Feb 16 '07 #4

Motoma
Expert 2.5K+
P: 3,235
thanks
eric
You can use this to do some tricky things, as a way to remove if(...) statements from your code:

[PHP]if($count > 50)
{
callFunction();
}[/PHP]

can be rewritten as:

[PHP]$count > 50 && callFunction();[/PHP]

which happens to be beautifully unreadable.
Feb 16 '07 #5

tolkienarda
100+
P: 316
ok i thought i might have understood the code in your post but now i am confused.
confused but interested if you know an atricle that talks about what you are posting or if you have the paitence to post somemore i would appreciate it

eric
Feb 16 '07 #6

Motoma
Expert 2.5K+
P: 3,235
ok i thought i might have understood the code in your post but now i am confused.
confused but interested if you know an atricle that talks about what you are posting or if you have the paitence to post somemore i would appreciate it

eric
I would love to elaborate on my little hack.

[PHP]
if( $count < 50 )
{
lessThanFifty();
}
[/PHP]

This snippet of code will check to see if $count is less than 50, and if so, the function lessThanFifty() will get executed.

[PHP]
$count < 50 && lessThanFifty();
[/PHP]

This snippet of code does the same thing.
EXPLANATION:
&& is a Logical AND operator. It will check the truth of the left side, the truth of the right side, and RETURN the LOGICAL AND of both sides.
If $count is 50 or above, && doesn't need to know the results of lessThanFifty(), because the LOGICAL AND of FALSE and anything is FALSE.
If $count is less than 50, it will continue and evaluate lessThanFifty(), and then return the LOGICAL AND of it and TRUE. We don't care about the result, because all we really wanted to do is execute lessThanFifty().

[PHP]
$count <= 50 || moreThanFifty();
[/PHP]
The LOGICAL OR of TRUE and anything is always TRUE. Therefore, when $cound it 50 or less, || will ALWAYS be TRUE, therefore, moreThanFifty() does not need to be executed.

If you are still curious, post back, I will be happy to answer all of your questions regarding this topic.
Feb 16 '07 #7

tolkienarda
100+
P: 316
i do understand it and i think it is very cool now if i were to inject into a form somthing along the lines of

1' || 'http://www.asdf.com/naughtycode.php?somthin

could i cause some trouble (not that i would ever do that)
eric
Feb 16 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.