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

Post by montyauto »

feyd wrote:Standard variables cannot be two, separate words (by spaces.) You need to look at arrays more or variable variables.
I have been looking from both array & variable variables
still found no way to get string from variable..
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

You would set up array keys, and then output the array key as the variable name and $array[$array_key] for the value.
User avatar
montyauto
Forum Commoner
Posts: 25
Joined: Sat Jan 13, 2007 7:05 am

Post by montyauto »

Ambush Commander wrote:You would set up array keys, and then output the array key as the variable name and $array[$array_key] for the value.
I can't get it, still blank idea..
would you pls show me that way with the code?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Code: Select all

$person['name'] = 'John';
$person['favorite color'] = 'gold';
print 'hey friend, ';
foreach ($person as $property => $value) {
  print "his $property is $value, ";
}
User avatar
montyauto
Forum Commoner
Posts: 25
Joined: Sat Jan 13, 2007 7:05 am

Post by montyauto »

Ambush Commander wrote:

Code: Select all

$person['name'] = 'John';
$person['favorite color'] = 'gold';
print 'hey friend, ';
foreach ($person as $property => $value) {
  print "his $property is $value, ";
}

thanks now I get what you mean

but, again, I need to write in the code:

Code: Select all

$myvar = 42;


not

Code: Select all

$something["myvar"] = 42;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's not going to happen. Why do you need this so specifically? What's the actual case you are dealing with?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Try...

Code: Select all

$vars = array('name', 'age');
$something = compact($vars);
$something is now an array you can iterate over.
User avatar
montyauto
Forum Commoner
Posts: 25
Joined: Sat Jan 13, 2007 7:05 am

Post by montyauto »

Finaly I just get the answer.
Last edited by montyauto on Sun Feb 25, 2007 4:15 pm, edited 1 time in total.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I seriously do not think this is the way you should be going about doing this. You've possibly introduced a security vulnerability and the code won't make much sense to other people.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

second thread on same topic.
see viewtopic.php?t=62067
User avatar
montyauto
Forum Commoner
Posts: 25
Joined: Sat Jan 13, 2007 7:05 am

Post by montyauto »

Ambush Commander wrote:I seriously do not think this is the way you should be going about doing this. You've possibly introduced a security vulnerability and the code won't make much sense to other people.
If it is the case then I should appologize.
I have edited my previous post.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

There's no need to remove the link, we're just trying to help you.
Post Reply