What does it mean to say that an expression suffers a short circuit?

1

In documentation of PHP, about operators says:

  

// foo () will never be called because the entire expression is short-circuited .

What does it mean to say that the whole expression is short-circuited? What happens to the script at run time?

<?php

// --------------------
// foo() nunca será chamada porque toda a expressão sofre curto circuito

$a = (false && foo());
$b = (true  || foo());
$c = (false and foo());
$d = (true  or  foo());
    
asked by anonymous 21.03.2017 / 14:13

1 answer

0

The short-circuit evaluation is something implemented by the compilers / interpreters to save resources and accelerate the evaluation of conditions. In a compound conditional test, if the first condition can guarantee the execution or not of the block.

For example:

  • with the variable $a , as the first condition is already false and the operator is a && (E): false And anything will always give false ;
  • With the variable $b , the first condition is true and the operator is a || (OU): true OR anything will always give true >;
  • examples $c and $d follow the same idea, respectively.

The link cited by @rray on operator precedence is quite valid!

I add the explanation of each operator's truth table here .

And also the specific link on short circuit, found here .

    
21.03.2017 / 14:48