PHP 5.3.3拼图,它打印的内容和原因

ope*_*rid 2 php puzzle ternary-operator

为什么它不打印什么打印?

<?php 
$place = 1; 
echo $place === 1 ? 'a' : $place === 2 ? 'b' : 'c'; 
?>
Run Code Online (Sandbox Code Playgroud)

Nik*_*kiC 8

手册是你的朋友.引用:

<?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.
Run Code Online (Sandbox Code Playgroud)

你基本上是这样做的:

echo ($place === 1 ? 'a' : $place === 2) ? 'b' : 'c';
// which is
echo 'a' ? 'b' : 'c';
// which is
echo 'b';
Run Code Online (Sandbox Code Playgroud)