note 58325 deleted from language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:36:59 +0000 |
| Subject: | note 58325 deleted from language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155564@lists.php.net to get a copy of this message | ||
Note Submitter: zapakh at yahoo dot com
----
Addendum to the post by tc underline at gmx TLD ch, on unsetting global variables from inside
functions:
If setting to null is not a suitable substitute for unset() in your application, you can unset the
global variable's entry in the $GLOBALS superglobal.
<?php
function testc()
{
global $a;
echo " inner testc: $a\n";
unset($GLOBALS['a']);
echo " inner testc: $a\n";
}
$a = 5678;
echo "<pre>";
echo "outer: $a\n";
testc();
echo "outer: $a\n";
echo "</pre>\n";
?>
/***** Output:
outer: 5678
inner testc: 5678
inner testc: 5678
outer:
******/
If the behavior of testc (or testa or testb, for that matter) seems surprising, consider that the
use of the 'global' keyword simply performs an assignment by reference. In other words,
<?php
global $a; //these two lines
$a =& $GLOBALS['a']; //are equivalent.
?>
If you've read http://php.net/references , then
everything behaves as you'd expect.