php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #26293 address operator combined with () or {} around array item gives Parse Error
Submitted: 2003-11-17 20:39 UTC Modified: 2004-07-26 17:04 UTC
From: mfoxx at hotmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 4.3.3 OS: RH 7.x
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mfoxx at hotmail dot com
New email:
PHP Version: OS:

 

 [2003-11-17 20:39 UTC] mfoxx at hotmail dot com
Description:
------------
The following parse error is reported to me:

Parse error: parse error, expecting `T_NEW' or `T_STRING' or `T_VARIABLE' or `'$'' 

when trying to do something like this:

$arr = array(array("a_1" => 1, "a_2" => 2), "b_1");
$a = &($arr[1]);

it complains about that the second line listed here.  You can see that I was trying to do was use () to force the operator precendence (binding) so that the & address operator binds to the whole array item reference, and not to the $arr first, and then the [].  Note that I also tried {} around it instead of () and got the same results.

Let me state that I have now read the operator precedence list and am aware of the fact that the [] will bind more tightly (first) before the &, so it was not necessary.

However, the documentation says you CAN use () to go around a part of an expression to force precedence, so doing so, even if technically not necessary, should not have caused an error.

In fact, you can do something like  $a = ((2 * 3) + 2) and there is no error produced, even though neither set of parentheses are actually needed.

And, when you just do:

$arr = array(array("a_1" => 1, "a_2" => 2), "b_1");
$a = &$arr[1];

there is no error and it works as expected.

So bascially, I think there is some problem with how the () around the array item reference is causing some type'ing problem for the & operator.

Reproduce code:
---------------
$arr = array(array("a_1" => 1, "a_2" => 2), "b_1");
$a = &($arr[1]);


Expected result:
----------------
i expected that this would compile with no parse error, and would be useable in my script.  I also expected that the $a variable would be an address reference to the single array item dereferenced by $a[1].

Actual result:
--------------
Parse error: parse error, expecting `T_NEW' or `T_STRING' or `T_VARIABLE' or `'$'' 


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-17 21:41 UTC] sniper@php.net
You can't do that. Should be documented somewhere..

 [2004-07-26 17:04 UTC] vrana@php.net
You can make references only to variables, not to expressions. It's written in the manual page language.references.whatdo: "PHP references allow you to make two variables to refer to the same content."
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 16:01:36 2025 UTC