= += -= etc. operators associativity

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pavenovak
Forum Newbie
Posts: 5
Joined: Fri Jan 31, 2003 10:16 am

= += -= etc. operators associativity

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Why don't you check oe php mailing lists.

Cheers,
BDKR (Terrence)
pavenovak
Forum Newbie
Posts: 5
Joined: Fri Jan 31, 2003 10:16 am

Post 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..?
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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:
Post Reply