"Guard"操作符就像PHP中的JavaScript一样

Kri*_*ves 14 javascript php

我喜欢在JavaScript中这样做:

function (a, b, c) {
    var foo = a || b || c;
    return foo.bar;
}
Run Code Online (Sandbox Code Playgroud)

有没有快速的方法来做回退或是否需要自定义function

小智 21

PHP 5.3引入了?:运算符(不要与三元条件混淆,去图).我不使用PHP,但我想它会是这样的:

 $foo = $a ?: $b ?: $c
Run Code Online (Sandbox Code Playgroud)

请参阅:http://php.net/manual/en/language.operators.comparison.php

从PHP 5.3开始,可以省略三元运算符的中间部分.表达式expr1?:expr3如果expr1的计算结果为TRUE则返回expr1,否则返回expr3.

快乐的编码.