“Guard” operator like JavaScript in PHP

前端 未结 1 920
花落未央
花落未央 2021-01-01 22:14

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 assignmen

相关标签:
1条回答
  • 2021-01-01 22:49

    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 讨论(0)
提交回复
热议问题