PHP Short Circuit Lazy Evaluation, Where Is It in the PHP.Net Manual

PHP short circuit lazy evaluation, where is it in the php.net manual?

Closest thing I can find to an 'official' mention of PHP's short-circuit implementation: http://php.net/manual/en/language.operators.logical.php

Does PHP stop looking after one 'or' (||) is met?

If the first one is true, then it will not need to check anything else in the statement so it will carry on. If not, code like this:

if(isset($var) && $var == 1)

Would never work as it would throw up a not defined error. As soon as it sees the first one is false, it stops the rest of the statement.

php syntax: program flow control using the || operator

It's just a boolean OR expression. The usage is taking advantage of a behavior called short cutting, where if the first part of the expression evaluates to true, then the second half isn't evaluated because the OR expression is already true.

PHP comparison nature - adaptive or oblivious?

<?php

if (print_r("Why don't you try?") || print_r("It's not that hard")) { }

?>

Does PHP have short-circuit evaluation?

Yes, the PHP interpreter is "lazy", meaning it will do the minimum number of comparisons possible to evaluate conditions.

If you want to verify that, try this:

function saySomething()
{
echo 'hi!';
return true;
}

if (false && saySomething())
{
echo 'statement evaluated to true';
}

Difference between && and and : Operator precedence and short circuiting

The code

$e = isset($a) and isset($b);

is parsed the same as

($e = isset($a)) and isset($b);

Therefore $e, as determined by isset($a) and the assignment, is true - independent of evaluating isset($b).

Processing of php 'if x && y'

What you're describing is known as "short-circuit evaluation".

Most languages work this way, including PHP, so they will evaluate an expression until they are certain of the result, and then stop, so the remainder of the expression would not be evaluated.

As you say, this is the most efficient approach.

However, it can potentially throw a spanner in the works for inexperienced programmers, who nay try something like this:

if(doFirstProcess() && doSecondProcess() {
print "both processes succeeded";
}

In this case, the programmer is expecting both functions to be called, but if the first one returns false, then the second one will not be executed, as the program already knows enough to be certain of the final result of the expression, so it short-circuits the remainder of the expression.

There are a few languages which don't do short-circuit evaluation. VB6 was one example (back in the day). I don't know about VB.Net, but since it's evolved from VB6, I would suspect it would be similar. But aside from that, all other languages that I've worked with have used short-circuit evaluation, including PHP.

There is a section in the PHP manual about this here: http://www.php.net/manual/en/language.operators.logical.php

And you can read more on short circuit evalution here: http://en.wikipedia.org/wiki/Short-circuit_evaluation

Hope that helps.

PHP comparison nature - adaptive or oblivious?

<?php

if (print_r("Why don't you try?") || print_r("It's not that hard")) { }

?>

Any difference between Lazy evaluation and Short-circuit evaluation?

The difference is that in case of lazy evaluation an expression is evaluated only when it is needed, while in case of short-circuit evaluation expression evaluation stops right after you know the result. It's sort of orthogonal notions.

Lazy evaluation can be applied to any computation (short-circuit scheme usually is used only with bools). It doesn't cut-off useless computation, but delays the whole computation until its result is required.

variable = bigAndSlowFunc() or evenSlowerFnc()
if (carry out heavy computations)
print "Here it is: ", variable
else
print "As you wish :-)"

If evaluation is lazy, variable will be computed only if we choose to go into the first (then) branch of if, otherwise it won't. At the evaluation stage (when we prepare arguments for print) short-circuit scheme can be used to decide if we need to call evenSlowerFnc.

So in your example, it's short-circuit evaluation since no delay of computation happen.



Related Topics



Leave a reply



Submit