note 44098 deleted from language.variables.scope by tomsommer
| From: | tomsommer@php.net | Date: | Thu, 15 Jul 2004 07:27:23 +0000 |
| Subject: | note 44098 deleted from language.variables.scope by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73261@lists.php.net to get a copy of this message | ||
Note Submitter: William At ScriptingBiz Dot Com - Scope
----
I love one liners.
The best one liner:
"I am" -- by the God of Abraham.
My Contribution to your code:
---------------------
<?php // Make ALL global variables available locally
function test(){
foreach ($GLOBALS as $var=>$val){global ${$var}; }
}
?>
---------------------
The point:
Run this in your function and every global variable is available by its name (i.e.
"$GLOBALS['a']" is "$a"). No more making a list of 50 variables to
pass to your function or put on a line after the "global" keyword.
Any variable not already defined in the global scope will be local to your function unless you
explicitly define it with the "global" keyword.
Keep in mind if you're working on a large project that any currently local variables in a
function can become global if you make a change and use it outside of your functions. For this
reason it's a good idea to use this in conjunction with a SOP of putting on a local prefix to
every local variable in a function (i.e. instead of $total use $local_total).
My photo gallery script is pure PHP/gd and this one liner simplified the passing of old/new,
height/width, source/destination and other values that went into many function calls.
--
It's easy to say a lot of words to convey a little information vaguely. It's much more
rewarding to say a few words to convey a lot of information concisely :-) .
-- by William Janoch (That's me!)