Re: variable names

From: Date: Wed, 15 Nov 2000 00:12:04 +0000
Subject: Re: variable names
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-25271@lists.php.net to get a copy of this message
Jan-Eric Duden writes: > Hi! > > Does anybody know if there is a function that returns the name of variable? > I imagine something like this: > > $myvar="bla"; > $name=GetVarName($myvar); > > $name=="myvar" > > Thks > Eric No. A given variable can have several names; which one do you want? For instance: <?php $foo = 'Bob'; $bar = &$foo; $baz = &$foo; $name = getvarname($baz); echo $name; // what should the output be? ?> In the above code, we end up with three names for the same slot in the symbol table: $foo, $bar, and $baz are all different names for the same value in memory. So should getvarname() return 'baz' in the above case? If so, the you have to have known the name to write the function call to start with. You may be able to emulate the effect you're after using variable variables; that way, you can just use a string variable as the variable name: <?php $varname = 'foo'; $$varname = 'Bob'; echo "$varname == $foo\n"; ?> ...which will output 'foo == Bob'. AFAIK that's about as close as you can get, though. -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#25271) next »