#34073 [NEW]: Is the array operator ([]) really right-associative?
| From: | phpbugs at majiclab dot com | Date: | Wed, 10 Aug 2005 16:53:37 +0000 |
| Subject: | #34073 [NEW]: Is the array operator ([]) really right-associative? | ||
| Groups: | php.doc | ||
| Request: | Send a blank email to phpdoc+get-969369541@lists.php.net to get a copy of this message | ||
From: phpbugs at majiclab dot com
Operating system: n/a
PHP version: Irrelevant
PHP Bug Type: Documentation problem
Bug description: Is the array operator ([]) really right-associative?
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 bug report at http://bugs.php.net/?id=34073&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=34073&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=34073&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=34073&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=34073&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=34073&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=34073&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=34073&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=34073&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=34073&r=support
Expected behavior: http://bugs.php.net/fix.php?id=34073&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=34073&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=34073&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=34073&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=34073&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=34073&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=34073&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=34073&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=34073&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=34073&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=34073&r=mysqlcfg