php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #7122 reference to array element problem
Submitted: 2000-10-10 12:41 UTC Modified: 2000-10-11 10:36 UTC
From: gargamel at wizard-labs dot org Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.2 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: gargamel at wizard-labs dot org
New email:
PHP Version: OS:

 

 [2000-10-10 12:41 UTC] gargamel at wizard-labs dot org
Creating a reference to an array element prevents that
particular element from being listed in a foreach-loop.
Instead, the previous element in the array is processed
twice.  Simple PHP script to reproduce the problem:

---------------
<?php
  $A = array(
    'a' => 'A',
    'b' => 'B',
    'c' => 'C');

  /* This works: */
  foreach ($A as $y => $x) {
    echo "y=$y x=$x\n";
  }

  /* This breaks: */
  $x = &$A['b'];
  foreach ($A as $y => $x) {
    echo "y=$y x=$x\n";
  }
?>
-------------------

This results in the following output:

---------------------
X-Powered-By: PHP/4.0.2
Content-type: text/html

y=a x=A
y=b x=B
y=c x=C
y=a x=A
y=b x=A     <== ERROR!!
y=c x=C
-------------------

Note:  I think this is possibly related to bug id #6417

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-10-11 10:36 UTC] stas@php.net
That's not array's problem, that's "$x"'s problem. You assign $x to be reference to $A['b'] and then use it as a loop variable, you get it's (and thus, $A['b']-s) contents overwritten. 
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sat Oct 10 02:00:02 2026 UTC