// 乍看起来下面的输出是 'true'
echo (true?'true':false?'t':'f');
// 然而,上面语句的实际输出是't',因为三元运算符是从左往右计算的
// 下面是与上面等价的语句,但更清晰
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.
/**
*先判断$_get['a']若成立则判断(isset($_get['b']) ? $_get['b'] : 'other'),因为有括号,所以
*会当成一个整体运算
*/
$rs = isset($_get['a']) ? $_get['a'] : (isset($_get['b']) ? $_get['b'] : 'other');
var_dump($rs);
/**
* 注意:自左向右结合运算
*先判断$_get['a']若成立则 则变成 $_get['a'] ? $_get['b'] : 'other';
*不可忽视括号的作用
*/
$rs = isset($_get['a']) ? $_get['a'] : isset($_get['b']) ? $_get['b'] : 'other';
var_dump($rs);
以上就是php中三元运算符的嵌套实例的详细内容。