php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42152 Operator Precedence: postincrement before '['
Submitted: 2007-07-30 23:26 UTC Modified: 2007-07-31 15:13 UTC
Votes:2
Avg. Score:3.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: php at tfwasmus dot enschedenet dot nl Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2007-07-30 (snap) OS: Windows Xp x64
Private report: No CVE-ID: None
 [2007-07-30 23:26 UTC] php at tfwasmus dot enschedenet dot nl
Description:
------------
According to the operator precedence (http://www.php.net/manual/en/language.operators.php#language.operators.precedence), a (post)increment should be evalued after the '['.

It seems that isn't true in assigning to an array in PHP5 anymore, while it was in PHP4. Latest snapshots of both checked (30-07-2007).

This could also be a documentation problem.

Reproduce code:
---------------
<?php
$k = 1;
$a = array();
$a[$k] = $k++;
var_dump($a,$k);
?>

Expected result:
----------------
array(1) {
  [1]=>
  int(1)
}
int(2)

Actual result:
--------------
array(1) {
  [2]=>
  int(1)
}
int(2)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-31 11:33 UTC] jani@php.net
Either I missed something but = has lower precedence than [ ?

 [2007-07-31 13:23 UTC] smlerman at gmail dot com
Last I knew, using a variable that you post-increment (and maybe pre-increment, can't remember for sure) elsewhere in the same statement results in undefined behavior.
 [2007-07-31 15:13 UTC] php at tfwasmus dot enschedenet dot nl
Hmm, after some consultation & a good night sleep I agree it's not a bug in operator precedence. It's more an undefined state of the operand which is the problem. 

Indeed: undefined behaviour, so the difference between PHP4 & PHP5 should not matter: a statement like this just shouldn't be used/relied upon.

The fact it's behaviour has changed between versions is however IMO enough reason to keep this in the bug database for people to find.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 14:01:30 2024 UTC