1

Basically

Is this:

<?PHP if (false && crazyFunction()) : ?>

The same as:

<?PHP if (false) : ?>
    <?PHP if (crazyFunction()) : ?>

If FALSE is evaluated in the first example will it still continue to evaluate "crazyFunction"?

Tyler Jones
  • 123
  • 7
  • 2
    Pretty much. See [this question on short circuiting in PHP](http://stackoverflow.com/questions/5694733/php-short-circuit-evaluation). – Waleed Khan Mar 11 '13 at 13:51
  • if always returns true, so if you value is false(int above example) it will execeute the code else it will execute the code from the "else part of the code" – Johny Mar 11 '13 at 13:52

2 Answers2

2

The && operator is a shotcircuit operator, which means that it will stop as soon as it knows the outcome is going to be false.

This means that if the left part evaluates to false it stops and returns false. crazyFunction() will never be called in this example.

Tarilo
  • 430
  • 2
  • 6
0

As soon as the value of boolean expression is known, it is no more executed.

Please note this (deprecated) example in old-fashioned mysql connections:

$db=mysql_connect_db('...') or die('Database error');

If after first part mysql_connect returned something that is not FALSE, 0, NULL etc., it does not execute this die().

(Regardless if this is or not correct to use mysql_* functions)

Voitcus
  • 4,463
  • 4
  • 24
  • 40