Re: while question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux