Page 1 of 1

Dynamic Variable Names

Posted: Tue Sep 20, 2005 1:21 pm
by Davos
Hey guys, new here and just started out in PHP.

stuck on a small loop im doing which assigns the value from a mysql query array to a variable. Every time the loop runs I want it to assign to variables with different names, increasing by one each time.

i am stuck on how to assign a variable name that contains an existing variable.

Code: Select all

$table_$x = $row[$x]
in theory i would want this to run through and assign variables named

$table_0, $table_1, $table_3 etc

and have their values as follows

$table_0 = $row[0]
$table_1 = $row[1]

this loop runs until $x = mysql_num_rows.

as you can see i am stuck on the part which assigns the variable names. i get syntax errors.

i have tried "$table_"."$x" and also $table_.$x but no luck.

could anybody point me in the right direction?

Posted: Tue Sep 20, 2005 1:31 pm
by raghavan20
see whether extract can be anyway useful to you.

Posted: Tue Sep 20, 2005 1:32 pm
by Weirdan
${"table" . $x}, if I remember correctly

Posted: Tue Sep 20, 2005 1:40 pm
by Davos
Weirdan wrote:${"table" . $x}, if I remember correctly
cheers mate, worked a treat.