note 44098 added to language.variables.scope
| From: | WilliamAtScriptingBizDotCom-Scope at rn2 dot php dot net | Date: | Thu, 15 Jul 2004 06:32:25 +0000 |
| Subject: | note 44098 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-73259@lists.php.net to get a copy of this message | ||
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!)
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44098
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44098&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44098&report=yes
Search -- http://master.php.net/manage/user-notes.php