1

">=" and "==" are non-associative operators and when they are next to each other, they get evaluated by precedence:

2 >= 3 == 3

it's like:

(2 >= 3) == 3 // ">=" have higher precedence over "=="

but if there is an associative operator in the mix, even lower precedence operator, it get evaluated acording to the associative operator:

var_dump($a = 2 >= $b = 3 == 3); // bool(true)
var_dump(2 >= 3 == 3); // bool(false)

it's ilke:

var_dump($a = 2 >= ($b = 3 == 3));
var_dump((2 >= 3) == 3); 

did i understood it correctly?

learning php
  • 1,134
  • 2
  • 10
  • 14

1 Answers1

1

PHP parses always only the defined ways. And giving variable assignments a higher (implicit) precedence is necessary as on the left of an assignment must be a variable. It's impossible to parse as ($a = 2 >= $b) = 3 == 3. It doesn't depend on the associativity.

Look at this example; the & operator is associative (and the => isn't).

$b = 2;
$a = 2 >= $b & 2;

In this case it is left to right. Like:

var_dump($a = ((2 >= $b) & 2)); // int (0)
var_dump($a = 2 >= $b & 2); // int (0)

Compare to:

var_dump($a = (2 >= ($b & 2))); // bool (true)
bwoebi
  • 23,637
  • 5
  • 58
  • 79