ID:               34073
 Updated by:       [EMAIL PROTECTED]
 Reported By:      phpbugs at majiclab dot com
-Status:           Open
+Status:           Closed
 Bug Type:         Documentation problem
 Operating System: n/a
 PHP Version:      Irrelevant
 New Comment:

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.




Previous Comments:
------------------------------------------------------------------------

[2005-08-10 18:53:36] 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


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=34073&edit=1

Reply via email to