Is there a shorter syntax for the ternary operator in php?

How can I create a shorter expression of:

$variable = @$array["property"] ? $array["property"] : DEFAULT_VALUE_CONSTANT;

To something like this:

$variable = @$array["property"] || DEFAULT_VALUE_CONSTANT;

Now I get true / false


Yes it's possible in PHP7 with Null coalescing operator ( ?? )

$variable = $array["property"] ?? DEFAULT_VALUE_CONSTANT;

If you are using PHP version < 7 one solution is use the elvis operator

$variable = $array["property"] ?: DEFAULT_VALUE_CONSTANT;

Please avoid using @ instead of isset() .

References:

?: operator (the 'Elvis operator') in PHP

链接地址: http://www.djcxy.com/p/57726.html

上一篇: 没有中间表达的三元陈述

下一篇: 在php中有三字运算符的较短语法吗?