variable variables [SOLVED]

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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

variable variables [SOLVED]

Post 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?
Last edited by s.dot on Sat Jul 08, 2006 4:22 pm, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

${$pollq.$i}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

I'm not sure, but I think he meant something like:

Code: Select all

${'pollq' . $i}
Not sure though.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

yeah,

Code: Select all

${'pollq'.$i}
is what i'm looking for
thanks feyd, ole, and oren :-D
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply