Re: [PHP4BETA] Trying to be elegant...
| From: | Lars Torben Wilson | Date: | Sun, 05 Mar 2000 21:44:23 +0000 |
| Subject: | Re: [PHP4BETA] Trying to be elegant... | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-11544@lists.php.net to get a copy of this message | ||
Johan Ekenberg writes:
> In the "elegant spirit of Perl", I tried to
> use the following code in PHP:
>
> ----
> while (list(,$val) = each(array("a", "b", "c"))) {
> print "$val<BR>\n";
> }
> ----
>
> It didn't work. I had to assign the array
> to a variable and pass the variable to each().
> Is there a way to write this in a more elegant,
> "Perl spirit" way in PHP?
>
> (This is obviously not a matter of high technical
> importance, but one of artistic and poetic freedom :-)
>
> Very best regards,
> /Johan Ekenberg
What the code above looks like it says is:
1. Create an array with elements 'a', 'b', and 'c';
2. Get the first element;
3. Assign that element to $val;
4. Print $val;
4. goto 1;
The each() is evaluated on every loop, as you intended, but so is the
array(), which I don't believe you want.
foreach() works though--just saw Zeev's message on this :).
Cheers,
Torben
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+