php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53870 Problem with passing array elements as references in foreach
Submitted: 2011-01-28 18:15 UTC Modified: 2011-04-17 11:29 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: lolbummer at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.5 OS: Mac OS X Snow Leopard
Private report: No CVE-ID: None
 [2011-01-28 18:15 UTC] lolbummer at gmail dot com
Description:
------------
I created an array of strings that would be typecast to integers, and cast them using a foreach loop, passing each element by reference.  After dumping the resulting array, it gave (somewhat) expected results, though after dumping each element individually, it gave an unexpected result on the last element.
This happened with PHP 5.3.5 on Mac OS X Snow Leopard, but did not happen on a Debian system with PHP 5.2.6.

Test script:
---------------
<?php
$array = array('1', '2', '5324', '435', '51');
foreach($array as &$element){
    $element = (int)$element;
}
var_dump($array);

foreach($array as $key => $element){
    var_dump($key);
    var_dump($element);
    echo "\n";
}
?>

Expected result:
----------------
array(6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(5324)
  [4]=>
  int(435)
  [5]=>
  int(51)
}
int(0)
int(1)

int(1)
int(2)

int(2)
int(3)

int(3)
int(5324)

int(4)
int(435)

int(5)
int(51)


Actual result:
--------------
array(6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(5324)
  [4]=>
  int(435)
  [5]=>
  &int(51)
}
int(0)
int(1)

int(1)
int(2)

int(2)
int(3)

int(3)
int(5324)

int(4)
int(435)

int(5)
int(435)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-04 16:04 UTC] uldericofilho at gmail dot com
On PHP 5.3.5 on CentOS 5.5 - same result. Proof of concept below:

echo "Result\n";
$arr = array(1,2,3,4,5);
foreach($arr as &$a){
	$a*=2;
}
foreach($arr as $a){
	echo $a."\n";
}

echo str_repeat('-', 10)."\nExpected\n";

$arr = array(1,2,3,4,5);
$arr = array_map(function($v){
	return $v*2;
}, $arr);
foreach($arr as $a){
	echo $a."\n";
}
 [2011-04-07 20:32 UTC] akhrulev at mail dot ru
I propose simplest testcase. It is reproduced on all my computers:
PHP 5.3.6 on Windows 7
PHP 5.3.5 on Windows XP
PHP 5.2.6 on Linux version 2.6.18-128.1.6.el5.centos.plus


<?php
$a = array( 'AAA', 'BB', 'C' );
foreach( $a as & $value )
{}
foreach( $a as $value )
{}
print_r( $a );
?>

Expected result:
Array
(
    [0] => AAA
    [1] => BB
    [2] => C
)

Actual result:
Array
(
    [0] => AAA
    [1] => BB
    [2] => BB
)
 [2011-04-17 08:57 UTC] chx@php.net
I have a much simpler reproduction script for, I think the same error.

$array = array('foo', 'bar');
foreach ($array as &$foo) {
}
foreach ($array as $foo) {
  echo "$foo\n";
}

Expected result:
----------------
foo
bar

Actual result:
--------------
foo
foo

Note that simply making $foo a reference is not enough. It is the double foreach on the same array, with the same variable that causes the reference to be "stuck".
 [2011-04-17 08:59 UTC] chx@php.net
Further testing reveals that an unset($foo); between the two seems to fix the problem.
 [2011-04-17 11:29 UTC] bjori@php.net
-Status: Open +Status: Bogus
 [2011-04-17 11:29 UTC] bjori@php.net
See the warnings and examples on http://no.php.net/manual/en/control-
structures.foreach.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 14:01:30 2024 UTC