php.net |  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
 [2015-02-20 15:17 UTC] rstoll at tutteli dot ch
Description:
------------
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:
http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_language_parser.y#777

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" (https://github.com/php/php-langspec/blob/b1e7a65fb9c985a8114322330468b77fb955cfae/spec/19-grammar.md#assignment-operators)
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;




Patches

Pull Requests

History

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] <https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#byref-assignment>
[2] <https://bugs.php.net/bug.php?id=68804>
 [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: Mon Dec 30 14:01:28 2024 UTC