Re: Strange PHP Reference behaviour with globals

From: Date: Fri, 16 Mar 2001 12:12:30 +0000
Subject: Re: Strange PHP Reference behaviour with globals
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-44256@lists.php.net to get a copy of this message
Thanks Yasuo, That might explain why $gGlobal is not permanently set to 99, but that doesn't explain why $gGlobal is set to 55 in ChangeGlobalValue() permanently. So are you or am I missing something? Bye, John ""Yasuo Ohgaki"" <yohgaki@hotmail.com> wrote in message news:OE73yz5b6KZmcK8r6be00000a44@hotmail.com... > This is expected behavior of PHP's reference. Reference works like a pointer, > but it dose NOT works like a pointer. This is case that reference does not > works as many programmer expected. > > I think this is in manual. > > Hint: when programmer use 'global $var'. It is the same as do '$var = & > $GLOBALS['var']'. > > Regards, > > Yasuo Ohgaki > ========================= > My favorite links > [RFC] http://www.faqs.org/rfcs/ [W3C] > http://www.w3.org/ > [PHP Manual] http://www.php.net/manual/en/ > > > Hello, > > > > Try the following code: > > > > <?php > > > > $gGlobal = 1; > > function ChangeGlobalValue() > > { > > global $gGlobal; > > > > $local = 55; > > $gGlobal = $local; > > print "Value:Global=$gGlobal<BR>"; > > } > > > > function ChangeGlobalRef() > > { > > global $gGlobal; > > > > $local = 99; > > $gGlobal = &$local; > > print "Ref:Global=$gGlobal<BR>"; > > } > > > > print "Global=$gGlobal<BR>"; > > ChangeGlobalValue(); > > print "Global=$gGlobal<BR>"; > > ChangeGlobalRef(); > > print "Global=$gGlobal<BR>"; > > ?> > > > > I get the following results: > > > > Global=1 > > Value:Global=55 > > Global=55 > > Ref:Global=99 > > Global=55 <=========== shouldn't this be 99 ??? > > > > Why does setting a global variable to a reference fail in a function? > > > > Tested on PHP 4.0.4 on IIS5 (CGI version). > > > > > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > > For additional commands, e-mail: php-general-help@lists.php.net > > To contact the list administrators, e-mail: php-list-admin@lists.php.net > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#44256) next »