Re: Determining the name of a global variable within a function

From: Date: Fri, 28 Jul 2000 17:20:30 +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-8819@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? Just do the obvious. Pass it in as: modify_array('foo'); And then in your function do: function modify_array($arg) { global $$arg; ${$arg}[] = 'added item'; } -Rasmus

« previous php.general (#8819) next »