PHP if/else 简写符号 - 多个条件

2023-12-27

请考虑以下代码构造:

condition  ? code_if_true  : 
condition2 ? code_if_true2 : 
             code_if_false;

这不适用于 PHP,但适用于 JavaScript。

有没有办法让它在 PHP 上工作?


In PHP, the conditional operator is left-associative[PHP.net] http://php.net/manual/en/language.operators.precedence.php#language.operators.precedence, compared to virtually all other languages where it is right-associative.

That's why you need to use parentheses to control the order of evaluation1:

 condition  ? code_if_true  : 
(condition2 ? code_if_true2 : 
              code_if_false ); 

1The order in which which operators are resolved, not when operands are evaluated. The latter is basically undefined[PHP.net] https://bugs.php.net/bug.php?id=61188#1330283084

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PHP if/else 简写符号 - 多个条件 的相关文章

随机推荐