RE: [PHP] trouble with each() list() and array()
| From: | Don Read | Date: | Mon, 20 Nov 2000 03:53:01 +0000 |
| Subject: | RE: [PHP] trouble with each() list() and array() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-26191@lists.php.net to get a copy of this message | ||
On 19-Nov-00 Christian Dechery wrote:
> I come from C, where pointers are everything, including the only way to
> store strings.
> And to store the form data I just parsed the QUERY_STRING (or post data)
> and stored into strings.
PHP handles all this for you:
http:// ... /my_app.php?var1=foo&var2=bar
echo $var1 ."<BR>\n";
echo $var2 ."<BR>\n";
will output:
foo
bar
> And I'm not quite getting these each() array() etc... functions...
> I'm not sure what an array is, in C is simple: an array is a pointer to a
> type.
> How do I store all the form data in a list for example?
> like in C you'd have like a struct
> struct form
> {
> char *field;
> char *value;
> }
>
> and then with a parser you'd create a list of struct form.
> But I'm not sure how that works in PHP.
>
PHP arrays are closer to Perl-style arrays; there are 2 types, flat &
associative (hash) arrays:
$xary= array('foo', 'bar', 'baz');
$dary= array('Sun' => 'Sunday', 'Mon' => 'Monday',
'Tue' =>'Tuesday');
echo $xary[1] ."<BR>\n";
echo $dary['Tue'] ."<BR>\n";
while (list($key, $val) =each ($xary)) {
printf('%s = %s <BR>', $key, $val);
}
while (list($key, $val) =each ($dary)) {
printf('%s = %s <BR>', $key, $val);
}
Produces:
bar
Tuesday
0 = foo
1 = bar
2 = baz
Sun = Sunday
Mon = Monday
Tue = Tuesday
knowing C is a fine base into Perl, then from Prel -> PHP; one of the stumbling
blocks for me was having to declare globals within functions.
Regards,
--
Don Read dread@texas.net
-- The problem with people who have no vices is that you can be
sure they're going to have some pretty annoying virtues.