<?php
echo 0 ?: 1 ?: 2 ?: 3;
echo 1 ?: 0 ?: 3 ?: 2;
echo 2 ?: 1 ?: 0 ?: 3;
echo 3 ?: 2 ?: 1 ?: 0;echo 0 ?: 1 ?: 2 ?: 3;
echo 0 ?: 0 ?: 2 ?: 3;
echo 0 ?: 0 ?: 0 ?: 3;
?>
每行将分别输出什么呢?能解释下么?

解决方案 »

  1.   

    本帖最后由 xuzuning 于 2010-06-18 11:32:44 编辑
      

  2.   

    Parse error: syntax error, unexpected ':' in ....语法错误
      

  3.   

    PHP官方的文档上是这样写的:http://www.php.net/manual/en/language.operators.comparison.php#95997
    我只是不明白是什么意思,所以问的。
      

  4.   

    echo 0 ? 0 : ( 1 ? 1 : (2 ? 2 : 3) );
      

  5.   

    升级以下,5.3开始支持中间省略首先要了解省略的如何工作
    a :? b这样的等价于a : a ? b然后文档里面关于多层的说明
    Note: It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious:Example #3 Non-obvious Ternary Behaviour
    <?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 0 ?: 1 ?: 2 ?: 3;则等价于echo (((0 ?: 1) ?: 2) ?: 3);
    首先由(0 ?: 1)等价(0 ? 0 : 1)得1
    ((0 ?: 1) ?: 2)即(1 ?: 2)等价(1 ? 1 : 2)得1
    (((0 ?: 1) ?: 2) ?: 3)即(1 ?: 3)等价(1 ? 1 : 3)得1