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?
Can I get a variable name as a string?
Moderator: General Moderators
- dibyendrah
- Forum Contributor
- Posts: 491
- Joined: Wed Oct 19, 2005 5:14 am
- Location: Nepal
- Contact:
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
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:
If I asked it to search for all variable names with the value of 6 it would return $var2?
Example:
Code: Select all
$var1 = 10;
$var2 = 6;I think what he is saying is if you had the fallowing:
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.
Code: Select all
$VarName = 'This var is a string!';- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
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.
maybe
But what do you need it for?
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";
}
}
?>