php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #34073 Is the array operator ([]) really right-associative?
Submitted: 2005-08-10 18:53 UTC Modified: 2005-08-30 16:51 UTC
From: phpbugs at majiclab dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: n/a
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phpbugs at majiclab dot com
New email:
PHP Version: OS:

 

 [2005-08-10 18:53 UTC] phpbugs at majiclab dot com
Description:
------------
I either don't understand why the [] operator is claimed to 
be right-associative, or there's a documentation error 
(http://ca.php.net/operators)

On that page, the following is claimed:

Operator: []
Associativity: Right

From my understanding of the online documentation, that 
means that for the following code:

<?php
$c = $a[1][2];
?>

The following "precedence" tree would be used:

1. [2]
2. [1]
3. =

But obviously it would be processed more like this:

1. [1]
2. [2]
3. =

The reasoning being that PHP needs to know what array you're 
accessing an index for, so the starts from the and goes on 
to the right.  If it started with [2], then it doesn't know 
what array to access yet until it accesses the next operator 
and says "oh ok, let's get index 2 from $a[1]".  So needless 
to say I am a little confused by the documentation?

In my mind, [] should either be non-associative or at least 
left-associative?

Now, I only just thought that it might be referring to 
nested []:

<?php
$a = $b[$c[1]];
?>

But in my mind, anything within [...] is a whole expression 
unto itself.  ie:

<?php
$a = $b[strtolower($c[1]) . '-stuff'];
?>

So I can understand where [] can be right-associative when 
nested, but in my mind [] is more closely related to () than 
anything else. () forces precedence, and everything inside 
of the () has not relation/cares to anything outside of it, 
same with [].  Anything inside the [] has nothing to do with 
anything outside of it.

Anyway, I just thought the documentation was possibly 
unclear on this, and I think it could be because of the 
complication of the [] compared to other operators.  For a 
final example:

<?php
$a = $b = 1;
$a[$b[1]];
?>

In both cases the operand precedence goes from 1 => $b => 
$a.  The operands are in the same order, but the [] wraps 
around the operands, whereas all the other operators do not.  
I think this could be the cause of the confusion or lack of 
specific examples/documentation on the website.

In any case, I think there should at least be some 
clarification, or maybe I am completely misunderstanding the 
[] operator?  I would hope not since I'm a fairly 
experienced PHP developer, but in any case I will admit that 
I may have the whole thing completely wrong!

Reproduce code:
---------------
n/a

Expected result:
----------------
n/a

Actual result:
--------------
n/a

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-08-30 16:51 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 20 19:00:03 2025 UTC