note 89872 added to language.variables.scope
| From: | LeighHarrison at osu1 dot php dot net | Date: | Thu, 26 Mar 2009 18:31:31 +0000 |
| Subject: | note 89872 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152483@lists.php.net to get a copy of this message | ||
External variables in a function
I needed to access dynamically-created variables from an included file within a helper function.
Because the list of $path_* variables I needed to access from the other file is itself dynamic, I
didn't want to have to declare all possible variables within the function, and I was concerned
at the overhead of declaring =all= members of $GLOBALS[] as global. However the following code
worked for me:
function makePath($root, $atom) {
$pos = strrpos($atom, '/');
if ($pos === false) {
global ${'path_'.$atom};
$path = ${'path_'.$atom};
}
else {
global ${'path_'.substr($atom, 0, $pos)};
$path = ${'path_'.substr($atom, 0, $pos)};
}
if ($path)
return ($pos === false)
? $root.$path
: $root.$path.substr($atom, $pos + 1);
else
return NULL;
}
Regards,
::Leigh
http://www.else.co.nz/
----
Server IP: 203.89.181.186
Probable Submitter: 118.93.33.219
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/89872
Del: integrated -- https://master.php.net/note/delete/89872/integrated
Del: useless -- https://master.php.net/note/delete/89872/useless
Del: bad code -- https://master.php.net/note/delete/89872/bad+code
Del: spam -- https://master.php.net/note/delete/89872/spam
Del: non-english -- https://master.php.net/note/delete/89872/non-english
Del: in docs -- https://master.php.net/note/delete/89872/in+docs
Del: other reasons-- https://master.php.net/note/delete/89872
Reject -- https://master.php.net/note/reject/89872
Search -- https://master.php.net/manage/user-notes.php