Strange PHP Reference behaviour with globals
| From: | John | Date: | Fri, 16 Mar 2001 10:48:36 +0000 |
| Subject: | Strange PHP Reference behaviour with globals | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-44249@lists.php.net to get a copy of this message | ||
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).