Two Ampersands Between Function Calls
What do two ampersands separating function calls do? Note: this is OUTSIDE any if statement or case statement
Like so:
开发者_如何学运维functionCallOne() && functionCallTwo();
it is equivalent to
if ( functionCallOne() ) {
functionCallTwo();
}
it just uses the short circuiting to make this 3 liner only take up one line
That is the short-circuit AND operator, meaning the second function (or expression/statement) will only be evaluated if the first returns true.
The logical AND operator &&
is a short-circuit operator. That means it will only move on to examine the second operand if the first operand is true
. If the first operand is false
, the whole expression can only be false
, so there's no point in checking the second operand, so it short-circuits out of the operation without having checked the second operand.
In this case this characteristic is used to mean "execute functionCallOne
and if that was successful execute functionCallTwo
".
The second function (with all it's side-effects) is not even executed if the first one returns false
, because PHP knows the whole expression can never be true
if one of them is false
.
精彩评论