Hi anyone. I found this note on php.net:
I recommend using a "while" instead of a "foreach," like this: <?php while (list($key, $val) = each($array)) { // Do something... } ?> This gives you more control and gets rid of some problems that you may have with the foreach statement.
I recommend using a "while" instead of a "foreach," like this:
<?php while (list($key, $val) = each($array)) { // Do something... } ?>
This gives you more control and gets rid of some problems that you may have with the foreach statement.
I'm wondering what are the problems using a foreach statement ? Could you give me an example of these troubles ? What's the best way to iterate over arrays ? Take care.
I don't know why anyone would recommend that over:
foreach($array as $key => $val) { // code }
To me, the foreach() just looks so much cleaner. And with it being such a popular function and all, I'm sure it's been optimized fairly well.
It's just legacy code: [man]foreach[/man] wasn't available until PHP 4.0.
It's been tweaked in PHP5, too:
foreach($array as &$val){}
does the Right Thing, where PHP4 spat a parse error.
Thanks a lot buddies. I can use foreach without troubles in my scripts if I got your posts right. I find it very useful. Bye.