开发者

"Guard" operator like JavaScript in PHP

I like to do this in JavaScript:

function (a, b, c) {
    var foo = a || b || c;
    return foo.bar;
}

Is there a quick way to do assignme开发者_运维技巧nt with fallback or does it require a custom function?


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

See: http://php.net/manual/en/language.operators.comparison.php

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.

Happy coding.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜