php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #17386 Associativity of assignment operators listed wrongly
Submitted: 2002-05-23 09:34 UTC Modified: 2004-07-26 16:51 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: m dot ford at lmu dot ac dot uk Assigned: john (profile)
Status: Closed Package: Documentation problem
PHP Version: 4.1.2 OS:
Private report: No CVE-ID: None
 [2002-05-23 09:34 UTC] m dot ford at lmu dot ac dot uk
The operators page at /manual/en/language.operators.php lists the the assignment operators (=, +=, etc.) as being left associative, but they must logically be right associative. Left associativity would mean that this:

    $a = $b = 0;

was interpreted as:

    ($a = $b) =0;

which is clearly nonsensical.  Indeed a quick test shows that:

    $d = $c += $a += $b += 4;

is interpreted as:

    $d = ($c += ($a += ($b += 4)));

which looks like right associativity to me!

(Actually, a little further testing suggests that the assignment operators need some special annotations, as they don't appear to strictly obey their stated precedence level.  For example, the statement:

    $x = $z + $y = 543;

if interpreted strictly according to the precedence rules should be equivalent to:

    $x = ($z + $y) = 543;

which would be a parse error, but in fact is interpreted as:

    $x = $z + ($y = 543);

which may be the "obvious" intention, but had me WTF-ing for a while!!)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-28 18:49 UTC] john@php.net
I'll make the change for the associtivity from left to right, but I don't know if I should just make a note of the example regarding precedence rules. 

Anyone know if its intended behavior, or should this have thrown a parse error?

John
 [2003-06-21 13:27 UTC] moriyoshi@php.net
The document was updated and now describes the assignment operators have right associativity. However I think the precedence rule is another issue.
 [2004-07-26 16:51 UTC] vrana@php.net
The precedence is also already documented by the sentence "Although ! has a higher precedence than =, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the output from foo() is put into $a."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 15:01:31 2024 UTC