代码之家  ›  专栏  ›  技术社区  ›  nik

异常三元操作

  •  14
  • nik  · 技术社区  · 14 年前

    我被要求执行三元运算符使用的操作:

    $test='one';
    
    echo $test == 'one' ? 'one' :  $test == 'two' ? 'two' : 'three';
    

    打印两个(用php检查)。

    我仍然不确定这个的逻辑。拜托,有人能告诉我这个的逻辑吗?

    7 回复  |  直到 14 年前
        1
  •  15
  •   DisgruntledGoat    14 年前

    嗯,是吗?和:具有相同的优先级,因此PHP将依次从左到右分析每个位:

    echo ($test == 'one' ? 'one' :  $test == 'two') ? 'two' : 'three';
    

    弗斯特 $test == 'one' 返回true,因此第一个parens的值为“one”。现在第二个三元的计算如下:

    'one' /*returned by first ternary*/ ? 'two' : 'three'
    

    “one”为true(非空字符串),因此“two”是最终结果。

        2
  •  7
  •   Krzysztof Bujniewicz    14 年前

    基本上,解释器从左到右对该表达式进行计算,因此:

    echo $test == 'one' ? 'one' :  $test == 'two' ? 'two' : 'three';

    被解释为

    echo ($test == 'one' ? 'one' :  $test == 'two') ? 'two' : 'three';

    parateheses中的表达式的计算结果为true,因为“one”和“two”都不是null/o/其他形式的false。 所以如果它看起来像:

    echo $test == 'one' ? FALSE :  $test == 'two' ? 'two' : 'three';

    它会印三张。为了使其正常工作,您应该忘记组合三元运算符,并对更复杂的逻辑使用常规的ifs/switch,或者至少使用括号,以便解释程序理解您的逻辑,而不是以标准的ltr方式执行检查:

    echo $test == 'one' ? 'one' :  ($test == 'two' ? 'two' : ($test == 'three' ? 'three' : 'four'));
    
    //etc... It's not the most understandable code... 
    
    //You better use:
    if($test == 'one')
        echo 'one';
    else { //or elseif()
    ...
    }
    
    //Or:
    switch($test) {
        case 'one':
            echo 'one';
            break;
        case 'two':
            echo 'two';
            break;
    //and so on...
    }
    
        3
  •  5
  •   Pekka    14 年前

    当您使用括号时,它工作正常:

    <?
     $test='one';
     echo $test == 'one' ? 'one' :  ($test == 'two' ? 'two' : 'three');
    

    我不完全理解,但对于口译员来说,没有括号,语句必须如下所示:

    echo ($test == 'one' ? 'one' :  $test == 'two') ? 'two' : 'three';
    

    第一个条件的结果似乎作为整个三元运算的结果返回。

        4
  •  1
  •   hamax    14 年前

    我认为是这样评价的:

    echo ($test == 'one' ? 'one' :  $test == 'two') ? 'two' : 'three';
    

    ($test='一'?'one“:$test='two')是非零/空的,因此'two'是逻辑输出

    如果您希望它正常工作,请写下:

    echo $test == 'one' ? 'one' :  ($test == 'two' ? 'two' : 'three');
    
        5
  •  1
  •   Felix Kling    14 年前

    PHP的 documentation 说:

    注: 建议您 避免“叠加”三元表达式 . 在一条语句中使用多个三元运算符时,PHP的行为并不明显:

    示例3不明显的三元行为

    <?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.
    ?>
    

    如果在错误的语句周围加上括号,它将打印 one :

    echo $test == 'one' ? 'one' :  ($test == 'two' ? 'two' : 'three');
    
        6
  •  1
  •   Kamil Szot    14 年前

    三元运算符是按照外观的顺序执行的,因此您真正具有:

    echo ($test == 'one' ? 'one' :  $test == 'two') ? 'two' : 'three';
    
        7
  •  0
  •   John Conde    14 年前

    嵌套的三元操作很恶心!上面的解释说明了原因。

    基本上这是逻辑:

    is $test == 'one'
    
      if TRUE then echo 'one'
    
      else is $test == 'two'
    
          if TRUE then echo 'two'
    
          else echo three