php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41364 Auto increment problem
Submitted: 2007-05-11 13:11 UTC Modified: 2007-05-11 22:41 UTC
From: wimroffel at planet dot nl Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.2.2 OS: Windows XP
Private report: No CVE-ID: None
 [2007-05-11 13:11 UTC] wimroffel at planet dot nl
Description:
------------
When I have a loop

  for($i=0; $i<$size;)
  { $myarr[$i][3] = $i++; 
  }

PHP will first take the assigned value, then increase $i and that use the new $i to set the array. So the first loop will do: 
$myarr[1][3]=0;

I come from PHP4 and there this worked correctly (first line $myarr[0][3]=0;). 

I am using XAMPP and as a consequence PHP 5.2.1. But I couldn't find this in the bug database so I suppose it has not been solved.

Reproduce code:
---------------
  $myarr = array();
  $myarr[0] = array("A","B","C");
  $myarr[1] = array("D","E","F");
  $myarr[2] = array("G","H","I");
  $size = sizeof($myarr);
  echo "The size of myarr is ".$size."<br>";
  for($i=0; $i<$size;)
  { $myarr[$i][3] = $i++; 
  }

  for($i=0; $i<$size; $i++)
  { echo "Record ".$i." = ".$myarr[$i][3]."<br>";
  }
echo "The size of the array is now ".sizeof($myarr);

Expected result:
----------------
The size of myarr is 3
Record 0 = 0
Record 1 = 1
Record 2 = 2
The size of the array is now 3

Actual result:
--------------
The size of myarr is 3
Record 0 = 
Record 1 = 0
Record 2 = 1
The size of the array is now 4

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-11 13:14 UTC] tony2001@php.net
This behaviour is undefined in all languages including PHP.
 [2007-05-11 22:41 UTC] wimroffel at planet dot nl
That sounds like a very weak excuse to change the working of the language and causing me and probably many others many hours of debugging.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 20:01:29 2024 UTC