"保安与QUOT;运算符,如PHP中的JavaScript [英] "Guard" operator like JavaScript in PHP
问题描述
我喜欢在JavaScript中执行此操作:
I like to do this in JavaScript:
function (a, b, c) {
var foo = a || b || c;
return foo.bar;
}
是否有快速方法可以使用后备进行分配,还是需要自定义 function
?
Is there a quick way to do assignment with fallback or does it require a custom function
?
推荐答案
PHP 5.3引入 ?:
运算符(不要与三元条件混淆,去图)。我不使用PHP,但我想它会是这样的:
PHP 5.3 introduces the ?:
operator (not to be confused with the ternary conditional, go figure). I don't use PHP, but I imagine it'd be something like:
$foo = $a ?: $b ?: $c
参见: http://php.net/manual/en/language.operators.comparison.php
从PHP 5.3开始,可以省略三元运算符的中间部分。表达式 expr1?:expr3 如果expr1的计算结果为TRUE则返回expr1,否则返回expr3。
Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.
快乐编码。
这篇关于"保安与QUOT;运算符,如PHP中的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!