Skip to content

Instantly share code, notes, and snippets.

@loganlinn
Created March 10, 2011 06:37
Show Gist options
  • Save loganlinn/863663 to your computer and use it in GitHub Desktop.
Save loganlinn/863663 to your computer and use it in GitHub Desktop.
Not-so-obvious PHP behavior when using for-each by reference and value with same variable name. (contrived example)
<?php
$foo = array(1, 2, 3);
foreach($foo as &$bar) {
$bar *= $bar;
}
foreach($foo as $bar){
echo "The square is ", $bar, "\n";
}
// Output:
// The square is 1
// The square is 4
// The square is 4
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment