note 58325 added to language.variables.scope

From: Date: Mon, 31 Oct 2005 16:23:47 +0000
Subject: note 58325 added to language.variables.scope
Groups: php.notes 
Request: Send a blank email to php-notes+get-97884@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/language.variables.scope.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58325 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58325&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58325&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58325&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58325&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58325&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58325&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58325&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58325&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97884) next »