了解嵌套的PHP三元运算符

时间:2011-06-03 07:43:08

标签: php ternary-operator operator-precedence

我不明白输出(“four”)是如何产生的?

$a = 2;

echo
  $a == 1 ? 'one'   :
  $a == 2 ? 'two'   :
  $a == 3 ? 'three' :
  $a == 5 ? 'four'  : 
    'other'  
  ;

// prints 'four'

我不明白为什么会打印“four”。

4 个答案:

答案 0 :(得分:26)

您需要包含三元条件:

<?php

for ($a=0; $a < 7; $a++) {
  echo (
    $a == 1 ? 'one' :
    ($a == 2 ? 'two' :
    ($a == 3 ? 'three' :
    ($a == 5 ? 'four' : 'other'))));
    echo "\n";
    // prints 'four'
}
exit;
?>

返回:

other
one
two
three
other
four
other

正如你所料。

请参阅PHP Ternary operator help处“三元运算符”底部的注释。

表达式从左到右进行评估。所以你实际上得到了:

  echo (
    ((($a == 1 ? 'one' : $a == 2)
     ? 'two' : $a == 3) ? 'three' :
    $a == 5) ? 'four' : 'other');

因此对于$a=2,您得到:

  echo (
    ((($a==2) ? 'two' : $a == 3) ? 'three' :
    $a == 5) ? 'four' : 'other');

然后

  echo (
    ((true ? 'two' : $a == 3) ? 'three' :
    $a == 5) ? 'four' : 'other');

然后

  echo (
    ('two' ? 'three' : $a == 5) ? 'four' : 'other');

然后

  echo (
    'three' ? 'four' : 'other');

所以echo 'four'

请记住,PHP是动态类型的,并将任何非零非空值视为TRUE。

答案 1 :(得分:6)

the Comparison Operators page in the PHP Manual上,他们解释了嵌套(堆叠)三元运算符时PHP的行为是"non-obvious"

你写的代码是这样的:

$a = 2;

echo
  ((($a == 1  ? 'one'   :
     $a == 2) ? 'two'   :
     $a == 3) ? 'three' :
     $a == 5) ? 'four'  : 
       'other'
  ;

// prints 'four'

由于$a为2且'two''three'都为TRUE,因此您会得到“four”,因为您不再进行比较如果'four'为真,则为

如果你想改变它,你必须把括号放在不同的地方[也注明:BeingSimpler和MGwynne]:

$a = 2;
echo 
  ($a == 1 ? 'one'   :
  ($a == 2 ? 'two'   :
  ($a == 3 ? 'three' :
  ($a == 5 ? 'four'  : 
     'other'))))
  ;

// prints 'two'

答案 2 :(得分:3)

分组条件存在问题,只需添加括号即可将它们分开。

$a = 2;
echo (
$a == 1 ? 'one' :
($a == 2 ? 'two' :
($a == 3 ? 'three' :
($a == 5 ? 'four' : 'other'))));
echo "\n";
// prints 'four'
exit;

解决。

答案 3 :(得分:1)

这是我想出来帮助自己理解三元运算符的左右相关性。

// PHP

$a = "T";
$vehicle =  $a == "B" ? "bus" :
            $a == "A" ? "airplane" :
            $a == "T" ? "train" :
            $a == "C" ? "car" :
            $a == "H" ? "horse" : "feet";

            // (as seen by the PHP interpreter)
            // INITIAL EXPRESSION: ((((($a == "B" ? "bus" : $a == "A") ? "airplane" : $a == "T") ? "train" : $a == "C") ? "car" : $a == "H") ? "horse" : "feet");
            // STEP 1:             (((((FALSE ? "bus" : FALSE) ? "airplane" : TRUE) ? "train" : FALSE) ? "car" : FALSE) ? "horse" : "feet")
            // STEP 2:             ((((FALSE ? "airplane" : TRUE) ? "train" : FALSE) ? "car" : FALSE) ? "horse" : "feet")
            // STEP 3:             (((TRUE ? "train" : FALSE) ? "car" : FALSE) ? "horse" : "feet")
            // STEP 4:             (("train" ? "car" : FALSE) ? "horse" : "feet")
            // STEP 5:             ("car" ? "horse" : "feet")
            // FINAL EVALUATION:   ("horse")

            // If you used the initial expression here (with the parenthesis) in a different language, it would also evaluate to "horse."

echo $vehicle; // gives us "horse"

这与以下相反:

// EVERY OTHER LANGUAGE

var a = "T";
var vehicle =   a == "B" ? "bus" :
                a == "A" ? "airplane" :
                a == "T" ? "train" :
                a == "C" ? "car" :
                a == "H" ? "horse" : "feet";

                // (as seen by the other language's interpreter)
                // INITIAL EXPRESSION: (a == "B" ? "bus" : (a == "A" ? "airplane" : (a == "T" ? "train" : (a == "C" ? "car" : (a == "H" ? "horse" : "feet")))));
                // STEP 1:             (FALSE ? "bus" : (FALSE ? "airplane" : (TRUE ? "train" : (FALSE ? "car" : (FALSE ? "horse" : "feet")))))
                // STEP 2:             (FALSE ? "bus" : (FALSE ? "airplane" : (TRUE ? "train" : (FALSE ? "car" : "feet"))))
                // STEP 3:             (FALSE ? "bus" : (FALSE ? "airplane" : (TRUE ? "train" : "feet")))
                // STEP 4:             (FALSE ? "bus" : (FALSE ? "airplane" : "train"))
                // STEP 5:             (FALSE ? "bus" : "train")
                // FINAL EVALUATION:   ("train")

                // If you used the initial expression here (with the parenthesis) in PHP, it would also evaluate to "train."

console.log(vehicle); // gives us "train"

如果您注意到,在PHP示例中,最里面的表达式位于左侧,而在第二个示例中,最里面的表达式位于右侧。每个步骤都会评估下一个最内层表达式,直到只有一个结果。如果您要在PHP中嵌套三元运算,则括号显然非常重要!