Re: while question
| From: | Craige Leeder | Date: | Mon, 17 Nov 2008 22:47:12 +0000 |
| Subject: | Re: while question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-283399@lists.php.net to get a copy of this message | ||
Alain Roger wrote:
Hi, i'm on PHP training and our lector is telling us that to avoid counting an array item amout thanks count($my_array), he tells we can do: while($my_array) { ... do something } but from experience this is an infinity loop... it should be always something like $count = count($my_array); while($i <= $count) { ... do something ... $i++; } has someone already use such syntax ? i mean as the first one. thx. While you teacher technically is wrong, you probably could implement something using the next() and current() array functions, though you're best to just use a foreach loop. foreach was designed specifically as an array looping construct, so it's optimized pretty well.Only thing to note with the foreach is that you are actually working on a copy of the array, so if you intend to modify it, pass it by reference. - Craige