Home  >  Article  >  Backend Development  >  三元运算问题

三元运算问题

WBOY
WBOYOriginal
2016-06-23 13:30:50853browse

$str = true?8888:true?100+100:100000000;echo '<br/>',$str;


为什么我这里结果输出的是200呢?  结合方向是左, 从第一三元运算符开始 为真 已经确定了 是8888 ,“:”后面的 不执行了的。难道不是这样?


回复讨论(解决方案)

$str = true?8888:(true?100+100:100000000); echo '<br/>',$str;


这样就对了

$c=1;$d=1;$str = true?8888:(true?$c++:$d++); echo $c,"<br/>";//1echo $d,"<br/>";//1echo '<br/>',$str;//8888

$str = (true?8888:true)?100+100:100000000; echo '<br/>',$str;//  200


如果不加括号系统是这样解释的。

23$str = (true?8888:true)?100+100:100000000;  echo '<br/>',$str;//  200


系统会这样解析?  三元运算符的 结合方向不是左吗?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn