Making a variable named after another var

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
mrpaplu
Forum Newbie
Posts: 3
Joined: Tue Oct 13, 2009 1:13 pm

Making a variable named after another var

Post by mrpaplu »

Hey there,

Is there a way to make a variable named after another var? I'm working on a project with an SQL-database, where I like to name a variable after a field of a table. I couldn't find an existing function to do this.

So for instance, the value of a field in my database is color. Now what I need is a way to get a variable that's named $color. This would help me a lot, less typing, much less. :D

Does anybody know a way to do this(or a function that does this)?

Gr. Mr.Paplu
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Making a variable named after another var

Post by Mark Baker »

Code: Select all

 
$row = mysql_fetch_assoc($queryResult);
foreach ($row as $name => $value) {
   $$name = $value;
}
 
So for instance, the value of a field in my database is color. Now what I need is a way to get a variable that's named $color. This would help me a lot, less typing, much less.
But it's still generally easier to work with an indexed array than it is with a set of dynamically named variables.... and normally it's more typing than less
Post Reply