4

Let's say we got a simple code like this:

// $foo and $bar aren't defined before

$foo = 5 && $bar = 15;

// var_dump()
// $foo is (bool) TRUE
// $bar is (int) 15

so I assume it works like:

$foo = (5 && ($bar = 15))

but in my opinion it should be:

$foo = ((5 && $bar) = 15) // should throw syntax error due FALSE = 15
  1. evaluation form left to right [$foo want 5 but && is higher]
  2. && got the highest priority [so && takes 5 and $bar]
    • 5 == TRUE; $bar == Undefined [so it's NULL == FALSE]
  3. = got the right associativity [waits for evaluation of (5 && $bar)]

Please explain it in easiest way (on some other examples) to poor man like me. Regards.

user2864740
  • 60,010
  • 15
  • 145
  • 220

2 Answers2

3

I think reading the manual page here helps and clears a lot of things.

So how does this get's evaluated?

$foo = 5 && $bar = 15;

First you have to know that && has a higher precedence than =. So the first thought would be this:

$foo = (5 && $bar) = 15;

But now is the point where you have to read the manual until the end: http://php.net/manual/en/language.operators.precedence.php

And a quote from there:

Note: Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.

What does that mean?

It silently assign 15 to $bar e.g.

$foo = (5 && ($bar = 15));

Now you can evaluate &&, $bar get's assigned with 15 and 5 && 15 is TRUE and get's assign to $foo

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • OK, so my problem is just an exception to the rule :-) @Rizier123 I didn't understand this 'note' in PHP manual now I do, thanks :-) – firstandlastpost Feb 19 '15 at 17:38
  • @firstandlastpost You're welcome! Have a nice day :D (FYI: If you didn't already saw it you can take a tour here: http://stackoverflow.com/tour and see how this site works :D (Welcome on SO!)) – Rizier123 Feb 19 '15 at 17:40
0

Lets dissect your line of code:

 $foo = 5 && $bar = 15;

First I'd like to point out that = is an assignment operator, and generally will return true.

That being said, this breaks down as follows:

$foo = (5 && ($bar = 15))
$foo = (5 && (true))
$foo = ( (true) && (true) )
$foo = (true)
$foo = true

&& stands for AND and compares two booleans, returning true if both are true, false if otherwise. Note that the assignment of $bar = 15 will return true, and in the case of 5, that non-zero integers also return true.

frost287
  • 81
  • 4
  • so the assignment operator has precedence over &&? – firstandlastpost Feb 19 '15 at 17:13
  • Its read from left to right. The first = is evaluated, but it can't evaluate until the right side is evaluated. So 5 && $bar = 15 needs to be evaluated. Next operator is &&, which needs 5 and $bar=15 to be evaluated. 5 evaluates to true and $bar=15 evaluates to true, then && evals to true and now the right side of the first = evals to true, assigning true to $foo. – frost287 Feb 19 '15 at 17:22
  • IMHO the && operator needs 5 and $bar.. so 5 (TRUE) and $bar (Undefined == NULL). Why PHP takes $bar = 15 if in documentation it says that && has higher precedence than =? – firstandlastpost Feb 19 '15 at 17:29
  • @firstandlastpost You're exactly right :D There is even a Note for this in the manual if you read it until the end – Rizier123 Feb 19 '15 at 17:33