I'm a bit of an optimizer (at least by my definition), and this question has been listening to me for quite some time.
I am wondering if PHP does some optimization on && and ||: Take the following example:
$a = "apple"; $b = "orange"; if ($a == "orange" && $b == "orange") { //do stuff }
When this code is executed, it checks to see if $ a is "orange". In this case, this is not so. However, there is an && operator. Since the first part ($ a == "orange") already returned false, will PHP still check if $ b is "orange?"
I have the same question for ||:
$a = "orange"; $b = "orange"; if ($a == "orange" || $b == "orange") { //do stuff }
When it checks if $ a is "orange", it returns true. Since this would do || return true statement, PHP will even check the second part || (since we already know that this will be true)?
I hope I have a point here, and I hope someone has an answer for me. Thanks!
source share