Page 1 of 1

Selecting variables dynamically?

Posted: Fri Jun 18, 2010 9:17 am
by hider
I don't know how best to explain this, but I have a few variables like this:

Code: Select all

$question1, $question2, $question3.. etc.
Now, I want to run a forloop, and select each 'question' variable respectively inside the for loop.. but I don't know how to do this?

The 'counter' variable in the forloop is $j, and I was thinking something along the lines of:

Code: Select all

$question$j
But it doesn't work.. any help?

Re: Selecting variables dynamically?

Posted: Fri Jun 18, 2010 9:28 am
by cpetercarter
Ah, variable variables!

Try

Code: Select all

${$question.$j}

Re: Selecting variables dynamically?

Posted: Fri Jun 18, 2010 9:33 am
by AbraCadaver
Yes, variable variables are cool, but normally if you need to use them, an array would be a better and easier solution. Where are these question variables coming from?

Re: Selecting variables dynamically?

Posted: Fri Jun 18, 2010 9:53 am
by hider
AbraCadaver wrote:Yes, variable variables are cool, but normally if you need to use them, an array would be a better and easier solution. Where are these question variables coming from?
Of course, I can just turn them into an array.. what was I thinking. Haha.

Cheers :)