Page 1 of 1

= += -= etc. operators associativity

Posted: Mon Feb 24, 2003 10:47 am
by pavenovak
Hi,
in PHP documentation is written, that operators
= += -= *= /= .= %= &= |= ^= ~= <<= >>=
have associativity LEFT. For example

Code: Select all

$x = 1;
$y = 2;
$z = 3;
$x = $y = $z
I expect that $y = $z executes first, result is 3 and than is 3 assigned to $x. (Like in C). But it's RIGHT associativity, don't you think so? LEFT associativity means, that $x = $y is made first and $z is assigned to it's result... What's the result? $x? $y? Temporary variable?

However, it's not mistake in documentation, in PHP source is '=' marked also as LEFT associative.
Can anybody tell me how exactly $x = $y = $z (and $x += $y += $z etc.) works? I know that finally $x and $y have the same value as $z :) but what does PHP do with that expression?

Thanks, Pavel.

Posted: Mon Feb 24, 2003 1:25 pm
by BDKR
Why don't you check oe php mailing lists.

Cheers,
BDKR (Terrence)

Posted: Wed Feb 26, 2003 8:13 am
by pavenovak
OK, I have found that this is currently an open documentation bug and the same question at http://marc.theaimsgroup.com/?l=phpdoc& ... 526328&w=2, but I think that it's not documentation bug. Assignment operators are in PHP source in files for bison/flex input marked also as LEFT associative... Maybe Zends parser is non-standard and they do some tricks so finally these operators looks like right associative..?

Posted: Wed Feb 26, 2003 8:28 am
by Stoker
I don't know what the correct naming of things are, but in most (all) languagesg it works like you said

$x = $y = $beer = 'sam adams';

And from the days when I started reading the bible (The Camel) I recall that being Left in perl, same as the PHP doc says.. It start at the right and "works its way left" or whatever, so the leftmost is the least assigned... and as far as I know that is called 'Precedense order left'..

Edit/add:
..uhm.. nevermind, I think I talked before my first cofee of the day..

assigns to the right, a = is right and assigns to the right side and it does everything on the right side.. yeah u're right something funny in the php docs.. = and += and such should be 'right' while + - . is left..

*groggy* :oops: