Skip to content

Instantly share code, notes, and snippets.

@acirtautas
Last active October 12, 2015 15:08
Show Gist options
  • Save acirtautas/4045329 to your computer and use it in GitHub Desktop.
Save acirtautas/4045329 to your computer and use it in GitHub Desktop.
Strange php array reference behaviour. [FIXED]
<?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;
}
@acirtautas
Copy link
Author

Fix is to add unset($value); after each direct value usage.
http://stackoverflow.com/questions/3307409/php-pass-by-reference-in-foreach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment