|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69091 assign by reference and math
Submitted: 2015-02-20 15:17 UTC Modified: 2015-02-20 21:00 UTC
From: rstoll at tutteli dot ch Assigned:
Status: Closed Package: PHP Language Specification
PHP Version: 5.6.6 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: rstoll at tutteli dot ch
New email:
PHP Version: OS:


 [2015-02-20 15:17 UTC] rstoll at tutteli dot ch
assign by reference in conjunction with arithmetic does not result in a syntax error as expected. The test script shows two examples. The first one is correct according to the current implementation, but should result in a parser error IMO. The second results in a parser error (how it should be). 

It seems like the parser has a wrong precedence. I think the problem is this line in the grammar:

The langspec does not mention operator precedence explicitly (which should be added IMO) but implicitly the =& operator can be found in the section "Assignment Operator" (
which is further below than +

Test script:
$a = &$a + 1;
$a = 1 + &$a;

Expected result:
parser error in both cases or the precedence explanation needs to be changed. 
Reading the spec I would assume the first line in the code above is equivalent to:
$a = (&$a + 1);
and not
($a = &$a) + 1;


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-20 19:18 UTC] cmbecker69 at gmx dot de
The language specification further states[1]:

| assignment-expression must be an lvalue, a call to a function
| that returns a value byRef, or a new-expression (see comment below
| regarding this).

However, $a+1 is neither of these, so $a =& ($a + 1) is invalid;
therefore ($a =& $a) + 1 is tried and successfully parsed.

This issue is closely related to #68804[2], BTW.

[1] <>
[2] <>
 [2015-02-20 21:00 UTC] rstoll at tutteli dot ch
-Status: Open +Status: Closed
 [2015-02-20 21:00 UTC] rstoll at tutteli dot ch
ok, if this is really intentional. Not very intuitive but fair enough. IMO the precedence table in the doc should be updated (but this is covered by [2] as mentioned in your comment). Additionally, the spec should have a precedence table as well. But maybe it was left out on purpose. I will write to the standard list concerning this point.

Thanks for your quick answer.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Mar 02 01:01:28 2024 UTC