Can I get a variable name as a string?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
montyauto
Forum Commoner
Posts: 25
Joined: Sat Jan 13, 2007 7:05 am

Can I get a variable name as a string?

Post 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?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Actually, you probably could implement something like this using get_defined_vars(), although I don't know why.
User avatar
montyauto
Forum Commoner
Posts: 25
Joined: Sat Jan 13, 2007 7:05 am

Post 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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

get_defined_vars() + array_search().

I'm still wondering why you want the variable's name. :?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, first we'd like to know what you're trying to do. There is probably a better way.
User avatar
montyauto
Forum Commoner
Posts: 25
Joined: Sat Jan 13, 2007 7:05 am

Post 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;
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Are you using register globals?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

To make Ambush Commander's point more blatant... is this information coming through the URL or a form?
User avatar
montyauto
Forum Commoner
Posts: 25
Joined: Sat Jan 13, 2007 7:05 am

Post 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?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

What if the variable name is two or more words? ;-) I think arrays are the way to go in this case.
User avatar
montyauto
Forum Commoner
Posts: 25
Joined: Sat Jan 13, 2007 7:05 am

Post 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..
Last edited by montyauto on Sun Feb 25, 2007 1:49 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Standard variables cannot be two, separate words (by spaces.) You need to look at arrays more or variable variables.
Post Reply