note 25427 added to function.unset
| From: | karel dot panek at empyreum dot cz | Date: | Mon, 23 Sep 2002 14:33:51 +0000 |
| Subject: | note 25427 added to function.unset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-37148@lists.php.net to get a copy of this message | ||
unset() seems to be buggy when used inside functions on global-ized variables.
This piece of code:
----8<----8<----
function foo()
{
global $bar;
unset($bar);
}
$bar = "initial value";
foo();
if(isset($bar))echo "bar has ".$bar." after foo\n";
else echo "bar has no value after foo\n";
----8<----8<----
has this output:
----8<----8<----
bar has initial value after foo
----8<----8<----
If there is some other function called from inside of foo() (after unset) and if this function also
globalizes bar and assings it some new value, this new value is then set correctly. Example:
----8<----8<----
function foofoo()
{
global $bar;
$bar = "a value from foofoo()"
}
function foo()
{
global $bar;
unset($bar);
foofoo();
}
$bar = "initial value";
foo();
if(isset($bar))echo "bar has ".$bar." after foo\n";
else echo "bar has no value after foo\n";
----8<----8<----
has this output:
----8<----8<----
bar has a value from foofoo() after foo
----8<----8<----
--
http://www.php.net/manual/en/function.unset.php
http://master.php.net/manage/user-notes.php?action=edit+25427
http://master.php.net/manage/user-notes.php?action=delete+25427
http://master.php.net/manage/user-notes.php?action=reject+25427