View Full Version : The variable variable name

19 Jul 2009, 09:16 PM
When echoing the following variable, I get no problems:


$cat just becomes $cat1, $cat2, $catdog, etc etc. (whatever $i is).

My question is, how do I access this variable variable name in a script? I need to write a loop, where it adds the specified number of variables to an array, but...

for ($i = 0; $i < $amount; $i++) {
array_push($array, $doc{$i});

...does not work.

I've fallen and I can't get up again,

20 Jul 2009, 04:45 AM
HI MagicT,

Try this:

for($i = 1; $i <= 100; $i++){

$myVar = "cat" . $i;

$$myVar = "The value of cat" . $i . " is put here!";


Using $$ you can name a variable then call it.

It's not in context above but it should point you in the right direction. Personally I steer clear of {} as they can be troublesome.

I hope that helps!