note 31853 added to language.references

From: Date: Tue, 06 May 2003 17:32:53 +0000
Subject: note 31853 added to language.references
Groups: php.notes 
Request: Send a blank email to php-notes+get-47593@lists.php.net to get a copy of this message
The first code snippet above is printing "Hello from Iteration 5" not only because PHP retains variables after a loop, but because you are assigning by reference ($first_var = &$var). When you do this, $first_var becomes an alias of $var. Whatever changes you make in $var will also change $first_var (they are actually the same). If you do: $first_var = NULL; for ($i=0;$i<5;$i++) { $var = "Hello from Iteration $i"; if (!$i) $first_var = $var; // Assigning by value } echo $first_var; Then you'll get "Hello from Iteration 0". As you can see, it's not only a matter of variable scoping. -- http://www.php.net/manual/en/language.references.php http://master.php.net/manage/user-notes.php?action=edit+31853 http://master.php.net/manage/user-notes.php?action=delete+31853 http://master.php.net/manage/user-notes.php?action=reject+31853

« previous php.notes (#47593) next »