Muntasir Muntasir - 1 year ago 68
PHP Question

Does PHP have short-circuit evaluation?

Given the following code:

if (is_valid($string) && up_to_length($string) && file_exists($file))

returns false, does the php interpreter still checks later conditions like the
? If so, then why does it do extra work when it doesn't have to?

Answer Source

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';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download