异常三元运算

发布于 2024-08-29 02:32:42 字数 186 浏览 8 评论 0原文

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

$test='one';

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

打印两个(使用 php 检查)。

我仍然不确定这的逻辑。请谁能告诉我这个的逻辑。

I was asked to perform this operation of ternary operator use:

$test='one';

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

Which prints two (checked using php).

I am still not sure about the logic for this. Please, can anybody tell me the logic for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

霞映澄塘 2024-09-05 02:32:42

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

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

第一个 $test == 'one' 返回 true,因此第一个括号的值为 'one'。现在第二个三元数的计算如下:

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

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

Well, the ? and : have equal precedence, so PHP will parse left to right evaluating each bit in turn:

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

First $test == 'one' returns true, so the first parens have value 'one'. Now the second ternary is evaluated like this:

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

'one' is true (a non-empty string), so 'two' is the final result.

予囚 2024-09-05 02:32:42

基本上,解释器从左到右评估该表达式,因此:

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

被解释

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

为 括号中的表达式评估为 true,因为“一”和“二”都不是 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...
}

Basically interpreter evaluates this expression from left to right, so:

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

is interpreted as

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

And the expression in paratheses evaluates to true, since both 'one' and 'two' are not null/o/other form of false.
So if it would look like:

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

It would print three. To make it work okay, you should forget about combining ternary operators, and use regular ifs/switch for more complicated logic, or at least use the brackets, for the interpreter to understand your logic, and not perform checking in standard LTR way:

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...
}
謸气贵蔟 2024-09-05 02:32:42

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

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

我不明白它 100% 但没有括号,对于解释器来说,语句必须如下所示:

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

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

It works correctly when you use brackets:

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

I don't understand it 100% but without brackets, to the interpreter, the statement must look like this:

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

the result of the first condition seems to be returned as the result of the whole ternary operation.

染年凉城似染瑾 2024-09-05 02:32:42

我认为它是这样评估的:

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

($test == 'one' ? 'one' : $test == 'two') 是非零/空,所以

如果你希望它工作, 'two' 是逻辑输出正确地写:

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

I think that it is evaluated like this:

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

($test == 'one' ? 'one' : $test == 'two') is non-zero/null, so 'two' is logical output

if you want it to work correctly, write:

echo $test == 'one' ? 'one' :  ($test == 'two' ? 'two' : 'three');
○愚か者の日 2024-09-05 02:32:42

PHP 的 文档 说:

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

示例 #3 非明显的三元行为

<前><代码>

如果在 false 语句两边加上括号,它会打印 one

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

PHP'S documentation says:

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

If you put parenthesis around the false statement, it prints one:

echo $test == 'one' ? 'one' :  ($test == 'two' ? 'two' : 'three');
一抹微笑 2024-09-05 02:32:42

三元运算符按出现顺序执行,因此您确实拥有:

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

Ternary operators are executed in order of appearance so you really have:

echo ($test == 'one' ? 'one' :  $test == 'two') ? 'two' : 'three';
臻嫒无言 2024-09-05 02:32:42

嵌套三元运算太恶心了!上面的解释说明了原因。

基本上是这样的逻辑:

is $test == 'one'

  if TRUE then echo 'one'

  else is $test == 'two'

      if TRUE then echo 'two'

      else echo three

Nested ternary operations are gross! The above explanation shows why.

Basically this is the logic:

is $test == 'one'

  if TRUE then echo 'one'

  else is $test == 'two'

      if TRUE then echo 'two'

      else echo three
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文