为什么不打印应该打印的内容?

<?php
$place = 1;
echo $place === 1 ? 'a' : $place === 2 ? 'b' : 'c';
?>

最佳答案

The manual is your friend。引用:

<?php
// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');

// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right

// the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');

// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.

您基本上是在做:
echo ($place === 1 ? 'a' : $place === 2) ? 'b' : 'c';
// which is
echo 'a' ? 'b' : 'c';
// which is
echo 'b';

关于php - PHP 5.3.3难题,其打印内容以及原因,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7766425/

10-12 12:18
查看更多