Can I get a variable name as a string?
Moderator: General Moderators
Can I get a variable name as a string?
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?
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?
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.
$something["myvar"] = 42;
array_search() will get you a key from that.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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.Ambush Commander wrote:Actually, you probably could implement something like this using get_defined_vars(), although I don't know why.
Pls tell me more..
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
correct this is possible, but i need to write it in mycode as :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.
$myvar = 42;
not
$something["myvar"] = 42;
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
cause i want to display the variable name as the code result:feyd wrote:I'm still wondering why you want the variable's name.
Code: Select all
$name = 'john';
$age = 42;Code: Select all
print "hey friend his $myvar1 is $name, his $myvar2 is $age years old";Code: Select all
hey friend his name is john, his age is 42 years old- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
as i mentioned before:Ambush Commander wrote:What if the variable name is two or more words?I think arrays are the way to go in this case.
i need to write it in mycode as :
$myvar = 42;
not
$something["myvar"] = 42;
anything whatever possible, show me pls..
Last edited by montyauto on Sun Feb 25, 2007 1:49 pm, edited 1 time in total.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Standard variables cannot be two, separate words (by spaces.) You need to look at arrays more or variable variables.