I'm doing some experimenting with the unset() (http://php.net/unset)
language construct in a PHP 5.2.1 installation. I did not find any
documentation on what happens to an identically named local variable's
value after an unset is performed.
Let me start with this example:
<?php
function dostuff() {
$a = 4;
echo ">in function (init): ".$a."<\n";
global $a;
/*CHANGEME*/ unset($a);
echo ">in function (after unset): ".$a."<\n";
$a = 3;
echo ">in function (after local assign): ".$a."<\n";
}
$a = 2;
dostuff();
echo ">in page: ".$a."<\n";
?>
The output is:
>in function (init): 4<
>in function (after unset): <
>in function (after local assign): 3<
>in page: 2<
So this basically means that the global $a is dereferenced by the
unset() call and the local $a gets reinitialized.
A different thing happens when we replace the /*CHANGEME*/ line with
unset using the $GLOBALS[] array (the recommended way of unsetting a
global variable from inside a function):
unset($GLOBALS['a']);
This time the output is:
>in function (init): 4<
>in function (after unset): 2<
>in function (after local assign): 3<
>in page: <
Notice that after the unset statement the global $a is properly unset
BUT the value of the local $a becomes 2, which was the value of the
global $a at the function entry point.
Are these behaviors documented somewhere or should't I rely on these
unset() side effects at all in my code?
Thanks,
Robert
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php