For Loop
8 answers - 376 bytes -

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
No.1 | | 710 bytes |
| 
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,
No.2 | | 543 bytes |
| 
for($i=1; $i<100; $i++) {
${'p'.$i.'name'} = 'whatever';
}
- jeff
20-Jun-06, at 6:14 PM, 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
No.3 | | 1429 bytes |
| 
Ray Hauge wrote:
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.
No.4 | | 582 bytes |
| 
Are you sure that you don't want an array? Arrays are normally much
better for this type of thing.
That said,
${"p{$i}name"} = 'foo';
David
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
No.5 | | 1625 bytes |
| 
Tuesday 20 June 2006 15:28, Adam Zey wrote:
Ray Hauge wrote:
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.
No.6 | | 1700 bytes |
| 
Thanks everyone. Always nice to know there is more than one direction
to go in.
Albert
Jun 20, 2006, at 4:52 PM, Ray Hauge wrote:
Tuesday 20 June 2006 15:28, Adam Zey wrote:
>Ray Hauge wrote:
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.
No.7 | | 833 bytes |
| 
Albert Padley wrote:
Thanks everyone. Always nice to know there is more than one direction
to go in.
A alternative to variable variables might use these:
'extract' looks like it might be right up your alley:
<?php
$vars = array();
for ($i=1; $i<100; $i++) { $vars["p".$i."name"] = $i; }
extract($vars);
?>
Dante
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
No.8 | | 1959 bytes |
| 
Tue, 2006-06-20 at 19:19, Albert Padley wrote:
Thanks everyone. Always nice to know there is more than one direction
to go in.
Albert
Jun 20, 2006, at 4:52 PM, Ray Hauge wrote:
Tuesday 20 June 2006 15:28, Adam Zey wrote:
>Ray Hauge wrote:
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.
Ahhh, got to love the fact that there's a question, yet no need for the
taker to actually understand the implications. Does anyone know if you
can get toilet paper with certs printed on it?
Cheers,
Rob.