List Info

Thread: note 61673 added to control-structures.foreach




note 61673 added to control-structures.foreach
user name
2006-02-09 06:42:49
Dangers with References

<?php
$months = array("Jan", "Feb",
"March");

foreach ($months as &$month) {
    $month .= "Beep";
}

print_r($months);

foreach ($months as $month) {
    printf("%sn", $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.forea
ch.php
Edit        -- http://master.php.net/manage/user-notes.php?action=
edit+61673
Delete: added to the manual -- htt
p://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.p
hp.net/manage/user-notes.php?action=delete+61673&report=
yes&reason=useless
Delete: non-english         -- http://mast
er.php.net/manage/user-notes.php?action=delete+61673&rep
ort=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://ma
ster.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )