Created
March 10, 2011 06:37
-
-
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)
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 | |
$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