Re: while question

From: Date: Mon, 17 Nov 2008 13:16:23 +0000
Subject: Re: while question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-283346@lists.php.net to get a copy of this message
On 17 Nov 2008, at 13:01, Alain Roger wrote:
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.
The while would work if you removed elements of $my_array inside the loop, but you would still need to be sure that it would eventually be empty. I would guess that your lecturers point is that you shouldn't call count on every iteration as it's a waste of time. He may also be confusing while for foreach in which case I'd leave because you're unlikely to learn anything from him. -Stut -- http://stut.net/

« previous php.general (#283346) next »