Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP || and && logical optimization

I'm a bit of an optimization freak (at least by my definition) and this question has been bugging me for quite a while.

I'm wondering if PHP does some optimization on && and ||: Take the following example:

$a = "apple";
$b = "orange";
if ($a == "orange" && $b == "orange") {
    //do stuff
}

When that code executes, it will check if $a is equal to "orange." In this case it isn't. However, there is an && operator. Since the first part ($a == "orange") already returned false, will PHP still check if $b is equal to "orange?"

I have the same question for ||:

$a = "orange";
$b = "orange";
if ($a == "orange" || $b == "orange") {
    //do stuff
}

When it checks if $a is equal to "orange," it returns true. Since that would make the || operator return true, will PHP even check the second part of the || (since we already know it will be true)?

Hopefully I am making sense here, and hopefully somebody has an answer for me. Thank you!

like image 565
Jacob Brunson Avatar asked Apr 08 '12 02:04

Jacob Brunson


People also ask

Is || and && the same?

The && and || Operators in JavaScript. If applied to boolean values, the && operator only returns true when both of its operands are true (and false in all other cases), while the || operator only returns false when both of its operands are false (and true in all other cases).

Does || MEAN AND or or?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool .

What is the difference between || and or in PHP?

Note The key difference in the working of the two operators and the nature are same. The bitwise OR operator sets the bit value whereas the logical OR operator sets true or 1 if either one of the conditions/bit value is 1 else it sets false or 0.

Does || or && have precedence?

The logical-AND operator ( && ) has higher precedence than the logical-OR operator ( || ), so q && r is grouped as an operand. Since the logical operators guarantee evaluation of operands from left to right, q && r is evaluated before s-- .


1 Answers

PHP uses short circuit evaluation with binary conditionals (such as &&, || or their constant equivalents), so if the result of evaluating the LHS means the RHS isn't necessary, it won't.

For example...

method_exists($obj, 'func') AND $obj->func();

...is an exploitation of this fact. The RHS will only be evaluated if the LHS returns a truthy value in this example. The logic makes sense here, as you only want to call a method if it exists (so long as you're not using __call(), but that's another story).

You can also use OR in a similar fashion.

defined('BASE_PATH') OR die('Restricted access to this file.');

This pattern is used often as the first line in PHP files which are meant to be included and not accessed directly. If the BASE_PATH constant does not exist, the LHS is falsy so it executes the RHS, which die()s the script.

like image 181
alex Avatar answered Oct 04 '22 20:10

alex