php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54930 Assigning a value inside a foreach leads to change a variable out of scope
Submitted: 2011-05-25 23:04 UTC Modified: 2011-05-26 00:28 UTC
From: dobrolyubov at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: Irrelevant OS: any
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: dobrolyubov at gmail dot com
New email:
PHP Version: OS:

 

 [2011-05-25 23:04 UTC] dobrolyubov at gmail dot com
Description:
------------
I'm not sure if it's the right behavior, but seems that PHP doing it wrong, see the test script.

Test script:
---------------
$v = array();
$data = array('v1' => '0');
foreach($data as $k => &$v)
	;

foreach(array('v2' => '1') as $k => $v)
	;

var_dump($data);



Expected result:
----------------
array(1) {
  ["v1"]=>
  &string(1) "0"
}


Actual result:
--------------
array(1) {
  ["v1"]=>
  &string(1) "1"
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-05-26 00:28 UTC] pierrick@php.net
-Status: Open +Status: Bogus
 [2011-05-26 00:28 UTC] pierrick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

more explanation here: 
http://schlueters.de/blog/archives/141-References-and-foreach.html
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 13:01:34 2025 UTC