PHP中or运算符的行为 [英] The behaviour of the or operator in PHP

查看:108
本文介绍了PHP中or运算符的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解or运算符的行为.请参见以下示例:

I'm trying to understand the behavior of or operator. Please see the below examples:

$e = false || true;
var_dump($e);

输出符合预期:bool(true);

$f = false or true;
var_dump($f);

输出符合预期:bool(false).我理解=的优先级高于Or,因此这就是$f分配给false的原因.

Output is as expected: bool(false). I understood this in a way that the = has a higher precedence than the Or, so that's why the $f is assigned to false.

但是下面的代码与我的想法完全相反.我认为$foo将被分配给5,然后与自身进行比较. 但是$foo仅在设置$foo时才被分配,这意味着它正在检查$foo之前是否已分配给任何东西,将5分配给它.

But the below code works quite opposite of what I thought. I thought that the $foo will be assigned to 5 and then compared to itself. But the $foo is getting assigned only when if the $foo is set that means it is checking if the $foo is assigned to anything before, assign 5 to it.

$foo or $foo = 5; 

谁能解释为什么会这样?

Can anyone explain why this is so?

推荐答案

基础知识:

  1. 赋值表达式会得出赋值.

  1. An assignment expression results in the assigned value.

那是什么意思? $foo = 'bar'是一个表达式,其中赋值运算符=为其分配一个值.表达式总是返回自身的值.就像表达式1 + 2得出值3一样,表达式$foo = 'bar'得出值'bar'.这就是为什么这样的原因:

What does that mean? $foo = 'bar' is an expression, in which the assignment operator = assigns a value. An expression always returns a value itself. Just like the expression 1 + 2 results in the value 3, the expression $foo = 'bar' results in the value 'bar'. That's why this works:

$foo = $bar = 'baz'; // which is: $foo = ($bar = 'baz');

  • 布尔运算是短路运算.如果不需要,双方都不会总是得到评估.总体上,true || false始终为true,因为左手操作数为true,所以整个表达式必须为true. false在这里甚至都没有得到评估.

  • Boolean operations are short-circuiting operations. Both sides are not always evaluated if they don't need to be. true || false is always true overall, since the lefthand operand is true, so the whole expression must be true. false is not even being evaluated here.

    运算符优先级决定了部分内容的顺序表达式分组为子表达式.较高优先级的运算符与它们的操作数分组在较低优先级的运算符之前.

    Operator precedence dictates in which order parts of an expression are grouped into sub-expressions. Higher precedence operators are grouped with their operands before lower precedence operators.

    因此:

    $e = false || true;
    

    正在评估

    false || true,这将导致将值true分配给$e. ||运算符的优先级高于=,因此false || true被分组为一个表达式(与($e = false) || true相对).

    false || true is being evaluated, which results in the value true, which is assigned to $e. The || operator has a higher precedence than =, therefore false || true is grouped into an expression (as opposed to ($e = false) || true).

    $f = false or true;
    

    现在or的优先级比=,这意味着赋值操作被分组为or之前的一个表达式.因此,首先对$f = false表达式求值,其结果为false(请参见上文).因此,您有了简单的表达式false or true,该表达式接下来被求值并生成true,但没人关心.

    Here now or has a lower precedence than =, which means the assignment operation is grouped into one expression before or. So first the $f = false expression is evaluated, the result of which is false (see above). So then you have the simple expression false or true which is evaluated next and results in true, but which nobody cares about.

    评估工作如下:

    1. $f = false or true;
    2. ($f = false) or true;  // precedence grouping
    3. false or true;         // evaluation of left side ($f is now false)
    4. true;                  // result
    

    现在:

    $foo or $foo = 5; 
    

    在这里,$foo = 5的优先级更高,被视为一个表达式.由于它出现在or运算符的右侧,因此仅在必要时才对表达式求值.这取决于最初的$foo.如果$footrue,则将完全不评估右侧,因为true or ($foo = 5)总体上必须为true.但是,如果$foo最初为假值,则将评估右侧并将5分配给$foo,这将导致5为真,这表示整个表达式为true ,无人问津.

    Here, again, $foo = 5 has a higher precedence and is treated as one expression. Since it occurs on the right side of the or operator, the expression is only evaluated if necessary. It depends on what $foo is initially. If $foo is true, the right hand side will not be evaluated at all, since true or ($foo = 5) must be true overall. If $foo has a falsey value initially though, the right hand side is evaluated and 5 is assigned to $foo, which results in 5, which is true-ish, which means the overall expression is true, which nobody cares about.

    1. $foo or $foo = 5;
    2. $foo or ($foo = 5);   // precedence grouping
    3. false or ($foo = 5);  // evaluation of left side
    4. false or 5;           // evaluation of right side ($foo is now 5)
    5. true;                 // result
    

    这篇关于PHP中or运算符的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆