3

If you have two consecutive conditions in PHP, do they both get evaluated, or is the if-case evaluation cancelled if the first condition already failed (so the result of the second condition wouldn't matter)?

if (condition1 && condition2) { }
Garrin
  • 541
  • 1
  • 3
  • 8
  • like this both conditions need to be true. if you use || it will be either of the conditions need to be true. so if first condition is false it indeed skips – kpp May 12 '14 at 12:58
  • 2
    In the case of `&&`, if the first condition fails, it skips the entire `if` section. If the first is true, it will move to the second. If second is true, it will enter the condition, else skip it – asprin May 12 '14 at 12:58
  • @Nic Possibly a duplicate, but the term "Short-Circuit Evaluation" was unknown to me before. Thanks to the answers here, not anymore. – Garrin May 12 '14 at 13:49
  • Hi @Garrin, no worries - I don't blame you for not knowing that term, or not finding that other question. However, because your question is practically the same as that other question, I've flagged this question as a duplicate. – Nic Wortel May 12 '14 at 14:09

4 Answers4

11

PHP uses Short Circuit Evaluation, so the second condition would not even be evaluated if the first one is false.

neelsg
  • 4,802
  • 5
  • 34
  • 58
3

Since you are using &&, if the left condition fail the right ones wont be evaluated

if (statement1 && statement2)
{
    //If the left part is false, the right part is not even checked
}


if (statement1 || statement2)
{
    //if the left part is false, the right part is checked
}
Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59
2

If the result of the first condition makes evaluating the second condition unnecessary, PHP won't evaluate it (the second condition). This is called Short-circuit evaluation, a term that is also meantioned in PHP's documentation on logical operators.

Here is a small example to prove it:

<?php

function foo() {
    echo 'Bar';
    return true;
}

if (false && foo()) { }

Because PHP uses Short-circuit evaluation, foo() will not be executed, and therefore Bar will not be printed on the screen.

Because 'Test' will never be echoed, you can conclude that if the first condition returns false, the second condition is never evaluated.

The same works for || (OR) operators, but the other way around:

<?php

function foo() {
    echo 'Bar';
    return true;
}

if (true || foo()) { }

Since the first condition is already true, there is no need to evaluate the second one. Again, foo() is not executed, so you'll get an empty screen.

Nic Wortel
  • 11,155
  • 6
  • 60
  • 79
1

Its an AND. And PHP is lazy as hell. If the first functions returns false, it will not touch the other functions. Its the same with OR. If you use it, PHP is lazy and only evaluates as long as necessary.

if(condition1() || condition2())

If condition1() returns true, condition2 will be untouched. Its nice to know for performance, always evaluate the fast functions first in that case.^^

Realitätsverlust
  • 3,941
  • 2
  • 22
  • 46