Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The behaviour of the or operator in PHP

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

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

Output is as expected: bool(true);

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

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.

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?

like image 554
Daric Avatar asked Aug 31 '12 10:08

Daric


People also ask

What is mean by || and && in PHP?

In PHP, expressions that use logical operators evaluate to boolean values. Logical operators include: or ( || ) and ( && ) exclusive or ( xor )

What is the purpose of or operator?

The OR operator is a Boolean operator which would return the value TRUE or Boolean value of 1 if either or both of the operands are TRUE or have Boolean value of 1. The OR operator is considered one of the basic logical operators along with AND and NOT in Boolean algebra.

What is the and operator in PHP?

Use the PHP AND operator ( and , && ) to combine two boolean expressions and returns true if both expressions evaluate to true; otherwise, it returns false . The logical AND operator is short-circuiting.


1 Answers

The basics:

  1. An assignment expression results in the assigned value.

    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'); 
  2. 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.

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

Therefore:

$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; 

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.

The evaluation works like this:

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 

Now:

$foo or $foo = 5;  

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 
like image 59
deceze Avatar answered Sep 21 '22 02:09

deceze