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

Post Reply
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
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Confusing. I guess no.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Indeed, your question is confusing.

Please explain more.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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?

Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post 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.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

It's bad idea to want this.
Think of planning of you application.
Post Reply