note 31145 deleted from language.functions by googleguy
| From: | googleguy@php.net | Date: | Mon, 24 Aug 2015 10:00:44 +0000 |
| Subject: | note 31145 deleted from language.functions by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-203432@lists.php.net to get a copy of this message | ||
Note Submitter: php at simoneast dot net
----
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.