note 31145 added to functions

From: Date: Fri, 11 Apr 2003 02:59:50 +0000
Subject: note 31145 added to functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-46480@lists.php.net to get a copy of this message
If you're frustrated by not having access to global variables from within your functions, instead of declaring each one (particularly if you don't know them all) there are a couple of workarounds... If your function just needs to read global variables... function aCoolFunction () { extract($GLOBALS); .... This creates a copy of all the global variables in the function scope. Notice that because it's a copy of the variables, changing them won't affect the variables outside the function and the copies are lost at the conclusion of the function. If you need to write to your global variables, I haven't tested it, but you could probably loop through the $GLOBALS array and make a "global" declaration for each one. Then you could modify the variables. Please note that this shouldn't be standard practice, but only in the case where a function needs access to all the global variables when they may be different from one call to another. Use the "global var1, var2..." declaration where possible. Hope that helps some people. Simon. -- http://www.php.net/manual/en/functions.php http://master.php.net/manage/user-notes.php?action=edit+31145 http://master.php.net/manage/user-notes.php?action=delete+31145 http://master.php.net/manage/user-notes.php?action=reject+31145

« previous php.notes (#46480) next »