6

Lets say I've got a pice of code that looks like this:

if( !isset($this->domainID) && !$this->getDomainID() ){
    return false;
}

Will the 2nd statement run if the first one is true? Because performance wise it would be stupid to get the ID from the database if I've already got it and there's a lot of other situation where the same apply. If it doesn't I'd have to nest them, am I right?

I don't know if there's an standard on how programming language work in these cases or if it's different in other languages then php. I tried googling it but I didn't really know what to search for in this case. As you can see I hade a quite hard time describing it in the title.

Hultner
  • 3,710
  • 5
  • 33
  • 43

4 Answers4

21

Yes. If the first is true, the second will be evaluated. Conversely, if the first is false, the second will not be evaluated. This can be a good place for micro optimizations and, as you noted, logical progression.

Per the comments, the inverse is true for OR conditions. So if the first expression is false the next will be evaluated and if the first expression is true the next will not.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
3

No, if the first is false, the second will not be evaluated.

What you're really talking about here is "lazy evaluation". It's often used in functional programming languages and can vastly improve the run-time. See here for more info: http://en.wikipedia.org/wiki/Lazy_evaluation

PHP does not use lazy evaluation but in conditionals like this it does at least stop before the second argument if the result is already clear.

Nick Brunt
  • 9,533
  • 10
  • 54
  • 83
  • I would say, it *can be seen* as a lazy evaluation, but it is more commonly referred to as [short-circuit evaluation][1]. [1] http://en.wikipedia.org/wiki/Short-circuit_evaluation – phimuemue Apr 28 '11 at 12:05
  • Thanks for the link, I was first confused with your answer as it at first glance seems to conflict with the other answers but after reading it through I understod that you basically said the same thing, just with some extra information. Edit: Thanks for the link phimuemue, it's good to know more about it as I switch languages often and how the function might differ. – Hultner Apr 28 '11 at 12:05
  • No problem, it's worth learning a bit about lazy evaluation. In 10 years time it'll be pretty standard amongst the top programming languages. – Nick Brunt Apr 28 '11 at 12:09
3

Answer is yes, you can see it by yourself by doing something like this:

#!/usr/bin/php                                                                  
<?PHP
function test1() {
    echo "inside test1\n";
    return false;
}
function test2() {
    echo "inside test2\n";
    return true;
}
echo "test1 first AND test2 second\n";
if (test1() && test2()) {
    echo "both passed\n";
}
echo "test2 first AND test1 second\n";
if (test2() && test1()) {
    echo "both passed\n";
}
echo "test1 first OR test2 second\n";
if (test1() || test2()) {
    echo "one passed\n";
}
echo "test2 first OR test1 second\n";
if (test2() || test1()) {
    echo "one passed\n";
}
?>
rasjani
  • 7,372
  • 4
  • 22
  • 35
0

Yes, you are using an AND so both conditions will be checked if the first is true (but not if the first is false).

If you had used an OR and the first condition was true, php wouldn't evaluate the second.

Twelve47
  • 3,924
  • 3
  • 22
  • 29