Tuesday, 13 August 2013

PHP - If (x OR y) Performance

PHP - If (x OR y) Performance

I have a performance related question regarding how PHP evaluates the OR
operator in a conditional.
I have a conditional that calls 2 functions, both returning booleans:
The first is a simple, fast function - simpleFunction()
The second is a more intensive function that queries the DB -
intensiveFunction()
I could write the conditional like this, so that if the first, simple
function returned TRUE, the second more intense function would not be
executed:
if ( simpleFunction() ) {
// Do Stuff
} elseif ( intensiveFunction() ) {
// Do the same stuff (redundant code)
}
My question is, when using and OR operator in a PHP conditional, if the
first condition (on the left of the operator) is TRUE, will the second
function (on the right side of the operator) be executed?
if ( simpleFunction() || intensiveFunction() ) {
//Do Stuff
}
This conditional will be running inside a loop, so I would like to avoid
running intensiveFunction() on every iteration.

No comments:

Post a Comment