Re: gloabl & reference behavior question?

From: Date: Fri, 22 Jul 2005 05:54:32 +0000
Subject: Re: gloabl & reference behavior question?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-219182@lists.php.net to get a copy of this message
On Wed, July 20, 2005 3:39 pm, Surendra Singhi said: > Hello, > > (1) > When I try this code: > <?php > > $var_global =" stuff"; > function f1() { > global $var_global; > $var_local = array(1,2); > $var_global =& $var_local; > } > f1(); > print_r($var_global); > ?> > I get the output: > > stuff > > where as I was expecting junk value or null. > > > My question is that, because the array is created locally and we > pass it by reference (without copying) so on the exit of function, its > value > is lost, but how does the global variable gets back its old value? > > Is it something which is not defined by the PHP language reference and is > implementation specific? And in this case instead of giving the global > variable some junk value or null value, the implementation decides to > store > the old values? PHP is not C. The & operator is not, as far as I know, defined for an array assignment operation. True, you can use & in the parameter list in some versions to keep PHP from copying the whole array. But that does not legitimatize what you are doing, I don't think. I could be 100% wrong. I've never even *TRIED* to use a reference to an array because I simply don't want to write code that confusing in the first place. That's why I use PHP to build web pages instead of C. YMMV -- Like Music? http://l-i-e.com/artists.htm

« previous php.general (#219182) next »