Last active
October 12, 2015 15:08
-
-
Save acirtautas/4045329 to your computer and use it in GitHub Desktop.
Strange php array reference behaviour. [FIXED]
This file contains hidden or 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('a', 'b', 'c'); | |
foreach($array as $value) { | |
echo $value; | |
} | |
echo PHP_EOL; | |
foreach($array as &$value) { | |
echo $value; | |
} | |
// The fix! | |
unset($value); | |
echo PHP_EOL; | |
foreach($array as $value) { | |
echo $value; | |
} |
This file contains hidden or 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
abc | |
abc | |
abc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fix is to add unset($value); after each direct value usage.
http://stackoverflow.com/questions/3307409/php-pass-by-reference-in-foreach