开发者

Two Ampersands Between Function Calls

开发者 https://www.devze.com 2023-01-24 02:11 出处:网络
What do two ampersands separating function calls do? Note: this is OUTSIDE any if statement or case statement

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消