Re: while question

From: Date: Mon, 17 Nov 2008 13:18:10 +0000
Subject: Re: while question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-283347@lists.php.net to get a copy of this message
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.
Probably the easiest way for doing this is using foreach: $a = array(1,2,3,4,5); foreach ($a as $i) { print $i."<br>"; } This outputs: 1 2 3 4 5 This for e.g. gives an infinite loop: $a = array(1,2,3,4,5); while ($a) { print $i."<br>"; $i++; } So you are right because $a will always evaluate as true since it not empty. For these kinds of questions you should read the manual first and then ask here. http://gr2.php.net/manual/en/control-structures.while.php -- Thodoris

« previous php.general (#283347) next »