note 14464 deleted from language.references.spot by sniper

From: Date: Tue, 30 Sep 2003 01:27:06 +0000
Subject: note 14464 deleted from language.references.spot by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-57356@lists.php.net to get a copy of this message
Note Submitter: ---- The above example is not demonstrating a bug, but it demonstrates that the global construct to declare a use of a global variable within a function is just creating a new local alias for the global variable. If you want to perform both changes of references in the Within the function, when you perform the following: global $AA, $BB; you actually bind two local variables names $AA and $BB to reference the same value as the global variables, so you actually perform the same thing as: $AA =& $GLOBALS[AA], $BB =& $GLOBALS[BB]; Then when you do this: $AA =& $BB, you could think that you make the global variable $AA reference the same value as what $BB is referencing. This is wrong. You only make the local variable $AA reference what the local variable $BB is referencing, and you don't change the binding of global variables. When you then return from the function, the local variables $AA and $BB references are destroyed, and the global variables have not changed. With PHP 4, you can force a function to change the binding of a global variable by performing the following: $GLOBAL[AA] =& $GLOBAL[BB] Note that when you perform the above code, this does not change the binding of local variables that were declared with the global construct. So $AA is still referencing the value that GLOBAL[AA] was referencing when you performed the "global $AA" statement... If you want that both the local variable $AA and the global variable reference now the same value, you shoud perform both: $AA =& $BB; $GLOBAL[AA] =& $GLOBAL[BB]; However you do not need to declare the $GLOBAL array in the function, because it is implicitly bound as a local variable within the function: function foo() { global $AA, $BB, $GLOBAL; $AA =& $BB; $GLOBAL[AA] =& $GLOBAL[BB]; } Local variables implicitly bound within any function are: $GLOBAL, $argc, $argv, any declared function parameter, and $this (for object methods).

« previous php.notes (#57356) next »