Re: Determining the name of a global variable within a function
| From: | Niklas Brunlid | Date: | Mon, 31 Jul 2000 09:14:38 +0000 |
| Subject: | Re: Determining the name of a global variable within a function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-9131@lists.php.net to get a copy of this message | ||
> I want to write a function that modifies a global array but returns a
> different value. Is there any way (in PHP3) to determine the global name of
> a variable passed to the function?
>
> for example:
>
> function modify_array($array) {
> /* code to modify $array */
> return $someValue;
> }
>
> $foo = array("A", "B", "C");
> $bar = modify_array($foo);
>
> /* $foo should now be modified */
>
>
> If I just knew what $foo was called I could use the $GLOBALS array to access
> it, but $foo may or may not always be named $foo... So how do I determine
> its name, or is there some other way to modify the global array from within
> a function?
This works in my scripts:
function modify_array(&$array) {
/* code to modify $array */
}
...
modify_array($anyVariable); // $anyVariable will be modified here.
I've never used the modify_array(&$anyVariable) way, but I suspect it'd
work the same way. The difference is that all variables passed to the
function above are passed by reference.
/ Niklas (who is really too tired to be at work right now... :) )