Page 1 of 1

Making a variable named after another var

Posted: Thu Oct 15, 2009 8:34 am
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

Re: Making a variable named after another var

Posted: Thu Oct 15, 2009 9:25 am
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