Page 1 of 1

variable variables [SOLVED]

Posted: Sat Jul 08, 2006 4:13 pm
by s.dot

Code: Select all

for($i=1;$i<6;$i++){
    if(!empty($pollq.$i)){
I have the form fields

pollq1
pollq2.... through pollq5

which when cleaned and checked are turned into $pollq1 through $pollq5

I want to loop through them and do

if(!empty(pollq1))
if(!empty(pollq2))

but I'm having problems appending the $i onto the $pollq, so my variable names would be $pollq1, $pollq2, $pollq3, etc

I've tried

Code: Select all

if(!empty($pollq.$i))
and

Code: Select all

if(!empty($$pollq.$i))
but both give me the error of an unexpected . (period)

do I need to use eval?

Posted: Sat Jul 08, 2006 4:16 pm
by feyd

Code: Select all

${$pollq.$i}

Posted: Sat Jul 08, 2006 4:18 pm
by Ollie Saunders
a great man once said:
if you have variable names ending in number, they should be part of an array
secondly I'm sure you'll be very interested to know about, the brilliantly titled, variable variable syntax

Posted: Sat Jul 08, 2006 4:18 pm
by Oren
I'm not sure, but I think he meant something like:

Code: Select all

${'pollq' . $i}
Not sure though.

Posted: Sat Jul 08, 2006 4:22 pm
by s.dot
yeah,

Code: Select all

${'pollq'.$i}
is what i'm looking for
thanks feyd, ole, and oren :-D