note 61673 added to control-structures.foreach
| From: | daniel dot oconnor at gmail dot com | Date: | Thu, 09 Feb 2006 06:42:49 +0000 |
| Subject: | note 61673 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-103346@lists.php.net to get a copy of this message | ||
Dangers with References
<?php
$months = array("Jan", "Feb", "March");
foreach ($months as &$month) {
$month .= "Beep";
}
print_r($months);
foreach ($months as $month) {
printf("%s\n", $month);
}
?>
Because $month is a reference to $months[2], iterating again with the same varible name causes
$months[2] to be overwritten! Oh no!
Ouput:
Array
(
[0] => JanBeep
[1] => FebBeep
[2] => MarchBeep
)
JanBeep
FebBeep
FebBeep
----
Server IP: 203.16.234.20
Probable Submitter: 203.63.126.9
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61673
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61673&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61673&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61673&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61673&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61673&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61673&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61673&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61673&report=yes
Search -- http://master.php.net/manage/user-notes.php