php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65248 using references on foreach
Submitted: 2013-07-12 04:02 UTC Modified: 2013-07-25 00:38 UTC
From: 791446794 at qq dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: Irrelevant OS: Win
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: 791446794 at qq dot com
New email:
PHP Version: OS:

 

 [2013-07-12 04:02 UTC] 791446794 at qq dot com
Description:
------------
PHP version: 5.3.13

EXAMPLE:
<?php
$a=array(
 'key'=>1
);
foreach($a as &$item)
{}

$b=array(
 'abc'=>2
);
foreach($b as &$item)
{}
var_dump($a);
?>
It will get "array(1) { ["key"]=> &int(2) } "

Buy use code like following
EXAMPLE:
<?php
$a=array(
 'key'=>1
);
foreach($a as &$item)
{}
unset($item);//unset the variable $item

$b=array(
 'abc'=>2
);
foreach($b as &$item)
{}
var_dump($a);
?>
It get the result "array(1) { ["key"]=> int(1) } ";


I think that when use refrence in foreach ,it better auto unset the refrenced ITEM and don't need use 'unset' explicitly .


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-17 16:10 UTC] arpad@php.net
-Package: Systems problem +Package: Scripting Engine problem
 [2013-07-25 00:38 UTC] tyrael@php.net
-Status: Open +Status: Not a bug
 [2013-07-25 00:38 UTC] tyrael@php.net
Per the warning at http://php.net/manual/en/control-structures.foreach.php, you 
should always unset() the iterator reference after the foreach to avoid this sort 
of issue, since PHP doesn't have block scope.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jun 08 23:01:27 2025 UTC