Dan Hastings
Dan Hastings

Reputation: 3280

Will an If Statement Stop Checking if the first OR condition is met in PHP?

I want to try and make my code as efficient as possible and from what ive read

if($process == true)

Will process faster than something that calls a function. I.e

if(count($total) == 100) 

So if i had an OR inside my if and the first condition was a simple boolean check and it turned out to be true would the second part of the condition still be checked anyway?

For example

$process = true;
if($process == true || count($total) == 100)

Would the count function still be called even though process is true and that is enough the make the condition pass.

Upvotes: 8

Views: 14555

Answers (5)

Samir Das
Samir Das

Reputation: 1908

It will work same as exactly logical operator works. For example foo() will never get called.

if (false && foo()) {

}

if (true  || foo()) {

}

Upvotes: 14

Shub
Shub

Reputation: 2704

In case of OR once it found a true statement it wont check further conditions,

In case of AND once it found a false statement it wont check further conditions. its known as lazy evaluation.

illustration:

<?php
$var=0;
if($var++ || $var++){//Since 0 means false in Php both conditions will be checked
//Do nothing
}
echo $var;//output :2

if we change condition to if(++$var || $var++) the first condition will return true hence next condition will not be checked thus it will print output as 1;

Upvotes: 5

Igor
Igor

Reputation: 91

If any statement inside a or is true other statements will not be checked and the if will be evaluated to true.

If you are checking a and statement all statements needs to evaluate to true and if any of them is false the stamen check will stop because the if is already false.

Just another important detail is that a function inside a if does not slow down the if check, what will slow down is what the function is doing and if it needs a lot of processing of course will be slower than just check a boolean but do not get afraid of use functions because they are very important in any system.

Upvotes: 0

Benoit Esnard
Benoit Esnard

Reputation: 2075

PHP has indeed a mechanic named short-circuit: http://php.net/manual/en/language.operators.logical.php#example-140

If the first operand of a logical OR is true, then the second part isn't evaluated, and the function isn't called.

Since comparing a variable to a boolean is faster than calling a function in PHP, this mechanic can help you to optimize, but never forget that premature optimisation is the root of all evil.

Upvotes: 15

Gerton
Gerton

Reputation: 686

(I remember this from C# but I'm quite sure it's the same in php)

This depends if you use the single | or double || I think if you used the single ones it would still go do the other one ( usefull for when theres a function like loggin behind it ) if you use double and the first one is true it'll skip the second condition.

(This could be the other way around, so best bet is to try it using Javascript eg. create 2 function each returning true after alerting something, and thest these with either 1 line or 2)

Upvotes: -2

Related Questions