note 117780 added to language.references.arent
| From: | (Juice) | Date: | Fri, 07 Aug 2015 18:57:34 +0000 |
| Subject: | note 117780 added to language.references.arent | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203287@lists.php.net to get a copy of this message | ||
Consider this block of code:
<?php
$arr = ['1', '2', '3', '4'];
foreach ($arr as &$i) {}
echo implode($arr, ', ')."\n";
foreach ($arr as $i) {}
echo implode($arr, ', ')."\n";
?>
which will output
1, 2, 3, 4
1, 2, 3, 3
although it seems no changes were made to the array.
The last item in the array gets overwritten, because reference is replaced by a copy in the second
iteration. In more detail, it gets overwritten first by a 1, then by 2, by 3 and again by a 3.
Make sure to do an unset($i) when you run an iteration by copy after an iteration by reference using
same variable names!
----
Server IP: 72.52.91.14
Probable Submitter: 93.77.231.181
----
Manual Page -- http://php.net/manual/en/language.references.arent.php
Edit -- https://master.php.net/note/edit/117780
Del: integrated -- https://master.php.net/note/delete/117780/integrated
Del: useless -- https://master.php.net/note/delete/117780/useless
Del: bad code -- https://master.php.net/note/delete/117780/bad+code
Del: spam -- https://master.php.net/note/delete/117780/spam
Del: non-english -- https://master.php.net/note/delete/117780/non-english
Del: in docs -- https://master.php.net/note/delete/117780/in+docs
Del: other reasons-- https://master.php.net/note/delete/117780
Reject -- https://master.php.net/note/reject/117780
Search -- https://master.php.net/manage/user-notes.php