Created
June 23, 2014 05:44
-
-
Save abznak/7c392f92005ac3fceff3 to your computer and use it in GitHub Desktop.
on PHP and foreach by reference
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
04:46:58 tims@saw ~ $ cat test.php | |
<?php | |
$a = array(1,2,3,4); | |
#http://www.php.net//manual/en/control-structures.foreach.php | |
# "Warning Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset()" | |
foreach ($a as &$b) { | |
$b = $b * 2; | |
} | |
foreach ($a as $b) { | |
print "$b\n"; | |
} | |
04:47:02 tims@saw ~ $ php test.php | |
2 | |
4 | |
6 | |
6 | |
04:47:07 tims@saw ~ $ php -v | |
PHP 5.4.4-14+deb7u11 (cli) (built: Jun 16 2014 09:46:53) | |
Copyright (c) 1997-2012 The PHP Group | |
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies | |
with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It makes complete sense when you think about it.