Dynamic Variable Names

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
Davos
Forum Newbie
Posts: 2
Joined: Tue Sep 20, 2005 1:12 pm

Dynamic Variable Names

Post 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?
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

see whether extract can be anyway useful to you.
Last edited by raghavan20 on Tue Sep 20, 2005 1:32 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

${"table" . $x}, if I remember correctly
Davos
Forum Newbie
Posts: 2
Joined: Tue Sep 20, 2005 1:12 pm

Post by Davos »

Weirdan wrote:${"table" . $x}, if I remember correctly
cheers mate, worked a treat.
Post Reply