Last active
December 31, 2015 09:09
-
-
Save jt2k/7964626 to your computer and use it in GitHub Desktop.
Can someone explain this to me?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$array = array(0,1,2,3); | |
foreach ($array as &$val) { | |
$val = $val + 1; | |
} | |
print_r($array); | |
foreach ($array as $foo) { | |
echo $foo; | |
} | |
echo "\n"; | |
foreach ($array as $val) { | |
echo $val; | |
} | |
echo "\n"; | |
/* Output: | |
Array | |
( | |
[0] => 1 | |
[1] => 2 | |
[2] => 3 | |
[3] => 4 | |
) | |
1234 | |
1233 | |
^ ????? | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the answer: http://stackoverflow.com/questions/8220399/php-foreach-pass-by-reference-last-element-duplicating-bug