Re: For Loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tuesday 20 June 2006 15:28, Adam Zey wrote:
> Ray Hauge wrote:
> > On Tuesday 20 June 2006 15:14, Albert Padley wrote:
> >> I have a regular for loop - for($i=1; $i<100; $i++)
> >>
> >> Within the loop I need to create variables named:
> >>
> >> $p1name;
> >> $p2name;
> >> $p3name;
> >> etc.
> >>
> >> The integer portion of each variable name needs to be the value of $i.
> >>
> >> I can't seem to get my syntax correct?
> >>
> >> Can someone point me in the right direction?
> >>
> >> Thanks.
> >>
> >> Albert Padley
> >
> > If you really want to keep the p?name syntax, I would suggest throwing
> > them in an array with keys.
> >
> > $arr["p1name"]
> > $arr["p2name"]
> >
> > Then that way you can create the key dynamically:
> >
> > $arr["p".$i."name"]
> >
> > Not pretty, but it works.
> >
> > Thanks,
>
> I haven't checked this, but couldn't you reference it as $arr["p$iname"]
> ? Is there a reason why variable expansion wouldn't work in this
> circumstance?
>
> If it does, you could make it easier to read by doing $arr["p{$i}name"]
> even though the {} aren't required. It'd be a lot easier to read than
> concatenations :)
>
> Regards, Adam.

Both of those ways work.  I think there's a question on the PHP Certification 
Exam about the different ways to work with strings.

-- 
Ray Hauge
Programmer/Systems Administrator
American Student Loan Services
www.americanstudentloan.com
1.800.575.1099

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux