7

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.

2

5 Answers 5

14

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.

12

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

if (false && foo()) {

}

if (true  || foo()) {

}
0
5

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;

2
  • is this common across all languages? Commented May 11, 2015 at 12:48
  • 1
    @DanHastings - No it isn't, you need to read the documentation for the languages you're using to check whether it uses lazy evaluation or not
    – Mark Baker
    Commented May 11, 2015 at 12:49
0

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.

-2

(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)

2
  • In PHP, | is a bitwise OR operator, and || is a logical OR operator
    – Mark Baker
    Commented May 11, 2015 at 12:47
  • In C# | is a bitwise operator as well. These perform operations on the data, so it will not exhibit the short-circuit behavior of actual logical comparison operators. Do not confuse the two, bitwise and comparison serve very different purposes. Commented Mar 7, 2022 at 13:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.