Page 1 of 1

Can I get a variable name as a string?

Posted: Sat Jan 13, 2007 10:29 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: Sat Jan 13, 2007 10:35 am
by dibyendrah
Confusing. I guess no.

Posted: Sat Jan 13, 2007 10:36 am
by feyd
Indeed, your question is confusing.

Please explain more.

Posted: Sat Jan 13, 2007 11:16 am
by impulse()
Do you mean that if I asked for all variable names that had either the values 1 2 5 3 2 6 7 8 then it would return the name of the variable holding those values.

Example:

Code: Select all

$var1 = 10;
$var2 = 6;
If I asked it to search for all variable names with the value of 6 it would return $var2?


Posted: Sat Jan 13, 2007 11:42 am
by Z3RO21
I think what he is saying is if you had the fallowing:

Code: Select all

$VarName = 'This var is a string!';
He wants to be able to have a second variable that has the name of the variable in this case 'VarName'. I don't know how to do this either though.

Posted: Sat Jan 13, 2007 12:15 pm
by aaronhall
It sounds like your problem would be a lot easier to solve if you were using arrays. Once you load those values into an array, you can use array_search() to find its key, and make reference to it that way.

Posted: Sat Jan 13, 2007 12:59 pm
by volka
maybe

Code: Select all

<?php
$foo = 'abc';
$myvar = '12345';
$bar = array(1,2,3,4,5);
$myvar2 = 12345;
$yadda = '12345';

foreach(get_defined_vars() as $name=>$value) {
	if ( '12345'===$value) {
		echo $name, " <br />\n";
	}
}
?>
But what do you need it for?

Posted: Sat Jan 13, 2007 1:30 pm
by xpgeek
It's bad idea to want this.
Think of planning of you application.