while

(PHP 4, PHP 5, PHP 7)

while loops are the simplest type of loop in
PHP. They behave just like their C counterparts. The basic form
of a while statement is:

while (expr)
statement

The meaning of a while statement is simple. It
tells PHP to execute the nested statement(s) repeatedly, as long
as the while expression evaluates to
TRUE. The value of the expression is checked
each time at the beginning of the loop, so even if this value
changes during the execution of the nested statement(s), execution
will not stop until the end of the iteration (each time PHP runs
the statements in the loop is one iteration). Sometimes, if the
while expression evaluates to
FALSE from the very beginning, the nested
statement(s) won't even be run once.

Like with the if statement, you can group
multiple statements within the same while loop
by surrounding a group of statements with curly braces, or by
using the alternate syntax:

while (expr):
statement
...
endwhile;

The following examples are identical, and both print the numbers
1 through 10:

User Contributed Notes 18 notes

Just a note about using the continue statement to forego the remainder of a loop - be SURE you're not issuing the continue statement from within a SWITCH case - doing so will not continue the while loop, but rather the switch statement itself.

While that may seem obvious to some, it took a little bit of testing for me, so hopefully this helps someone else.

function write_fix ($lock_file) { while( file_exists($lock_file){// do something in here? // maybe sleep for a few microseconds // to maintain stability, if this is going to // take a while ?? [just a suggestion]} }

?>

This method is not recommended for use with programs that will be needing a good few seconds to write to a file, as the while function will eat up alot of process cycles. However, this method does work, and is easy to implement. It also groups the writing functions into one easy to use function, making life easier. :-)

Just a note to stuart - the reason for this behaviour is because using the while(value = each(array)) construct increments the internal counter of the array as its looped through. Therefore if you intend to repeat the loop, you need to reset the counter. eg:

> So in effect the main while loop is only doing one iteration... and not 4 as expected....

That's the wrong conclusion. The outer "while" does all four iterations. However the "inner" loop does nothing for the second, third and fourth run.

> I think it would be good to have an explaination of this strange behaviour.

Here it is:

<?PHP$i=0;while($i < count($one)) {

while($a = each($two)) { echo $a[1]." - ".$one[$i].", "; }$i++;

}?>

The "problem" is your use of "each", which reached the last item after the first iteration of the outer loop. After that, when you come back to the second iteration with the outer loop, "each" still is at the end of the array $two.

If you add a reset($two) in front of the inner "while", you'll get the result you expect.

$array = $save;test_time("",1);reset($array);while ($next = next($array)){ $key = key($array); $array[$key] = $array[$key] * 100;} test_time("while (\$next = next(\$array))");*********************************************************The improvement seems huge, but it isnt that dramatic in real practice. Results varied... I have a very long bidimensional array, and saw no more than a 2 sec diference, but on 140+ second scripts. Notice though that you lose control of the $key value (unless you have numeric keys, which I tend to avoid), but it is not always necessary.

I generally stick to foreach. However, this time, I was getting Allowed Memory Size Exceeded errors with Apache. Remember foreach copies the original array, so this now makes two huge 2D arrays in memory and alot of work for Apache. If you are getting this error, check your loops. Dont use the whole array on a foreach. Instead get the keys and acces the cells directlly. Also, try and use unset and Referencing on the huge arrays.

Working on your array and loops is a much better workaround than saving to temporary tables and unsetting (much slower).

I find it often clearer to set a simple flag ($finished) to false at the start of the loop, and have the program set it to true when it's finished doing whatever it's trying to do. Then the code is more self-documenting: WHILE NOT FINISHED keep going through the loop. FINISHED EQUALS TRUE when you're done. Here's an example. This is the code I use to generate a random filename and ensure that there is not already an existing file with the same name. I've added very verbose comments to it to make it clear how it works: