Page 1 of 2

Can I get a variable name as a string?

Posted: Sun Feb 25, 2007 11:46 am
by montyauto
I've searching in php and perl for this:

how do i get a variable name as a string e.g i have a variable
myvar:1 2 5 3 2 6 7 8
i want to get "myvar"

Is there any function to get the name of a variable based on its value?

Posted: Sun Feb 25, 2007 11:53 am
by jayshields
I can't think of any time when you would need to do this. It's possible, but unnecessary. What are you trying to achieve?

Posted: Sun Feb 25, 2007 12:17 pm
by feyd
There is no function to do it. There will likely never be a (built-in) function to do it.

Why do you want the variable's name?

Posted: Sun Feb 25, 2007 12:32 pm
by Chris Corbyn
I think what you want is a hashmap... which would be an associative array in PHP.

$something["myvar"] = 42;

array_search() will get you a key from that.

Posted: Sun Feb 25, 2007 12:34 pm
by Ambush Commander
Actually, you probably could implement something like this using get_defined_vars(), although I don't know why.

Posted: Sun Feb 25, 2007 12:58 pm
by montyauto
Ambush Commander wrote:Actually, you probably could implement something like this using get_defined_vars(), although I don't know why.
I've tried to do with get_defined_vars(), cannot go through.
Pls tell me more..

Posted: Sun Feb 25, 2007 12:59 pm
by feyd
get_defined_vars() + array_search().

I'm still wondering why you want the variable's name. :?

Posted: Sun Feb 25, 2007 12:59 pm
by Ambush Commander
Well, first we'd like to know what you're trying to do. There is probably a better way.

Posted: Sun Feb 25, 2007 1:01 pm
by montyauto
d11wtq wrote:I think what you want is a hashmap... which would be an associative array in PHP.

$something["myvar"] = 42;

array_search() will get you a key from that.
correct this is possible, but i need to write it in mycode as :

$myvar = 42;

not

$something["myvar"] = 42;

Posted: Sun Feb 25, 2007 1:02 pm
by Ambush Commander
Are you using register globals?

Posted: Sun Feb 25, 2007 1:04 pm
by Chris Corbyn
To make Ambush Commander's point more blatant... is this information coming through the URL or a form?

Posted: Sun Feb 25, 2007 1:33 pm
by montyauto
feyd wrote:I'm still wondering why you want the variable's name. :?
cause i want to display the variable name as the code result:

Code: Select all

$name = 'john';
$age = 42;
I want to display it later using:

Code: Select all

print "hey friend his $myvar1 is $name, his $myvar2 is $age years old";
it will display

Code: Select all

hey friend his name is john, his age is 42 years old
is it possible?

Posted: Sun Feb 25, 2007 1:34 pm
by Ambush Commander
What if the variable name is two or more words? ;-) I think arrays are the way to go in this case.

Posted: Sun Feb 25, 2007 1:44 pm
by montyauto
Ambush Commander wrote:What if the variable name is two or more words? ;-) I think arrays are the way to go in this case.
as i mentioned before:

i need to write it in mycode as :

$myvar = 42;

not

$something["myvar"] = 42;

anything whatever possible, show me pls..

Posted: Sun Feb 25, 2007 1:48 pm
by feyd
Standard variables cannot be two, separate words (by spaces.) You need to look at arrays more or variable variables.