Edit report at https://bugs.php.net/bug.php?id=65248&edit=1

 ID:                 65248
 Updated by:         tyr...@php.net
 Reported by:        791446794 at qq dot com
 Summary:            using references on foreach
-Status:             Open
+Status:             Not a bug
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 Operating System:   Win
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2013-07-12 04:02:32] 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 .



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65248&edit=1

Reply via email to