W3docs

PHP Looping with While Loop

The while loop in PHP allows developers to repeat a block of code multiple times as long as a certain condition is met. This type of loop is commonly used to

The while loop in PHP repeats a block of code as long as a given condition stays true. It is the right tool when you don't know in advance how many iterations you need — for example, reading rows from a database until there are none left, or asking for input until it's valid. When you do know the iteration count up front, a for loop is usually clearer.

This article covers the while loop's syntax, how its condition is evaluated, the alternative endwhile form, infinite loops and how to break out of them, and how while differs from the related do...while loop.

The Structure of the While Loop

The basic structure of the while loop in PHP is as follows:

PHP while loop syntax

while (condition) {
  // code to be executed
}

The condition is any expression that PHP evaluates to a boolean (true or false). The loop works in three steps:

  1. The condition is evaluated before each iteration.
  2. If it is true, the body runs once.
  3. PHP returns to step 1 and re-evaluates the condition.

This is important: because the test happens first, the body runs zero times if the condition is false to begin with. Something inside the loop (or the condition itself) must eventually make the condition false, otherwise the loop runs forever — see Infinite Loops below.

Alternative endwhile syntax

PHP also offers a colon-and-endwhile form, which is handy when you mix loops with HTML in a template:

<?php $i = 1; ?>
<ul>
<?php while ($i <= 3): ?>
  <li>Item <?php echo $i; ?></li>
  <?php $i++; ?>
<?php endwhile; ?>
</ul>

Both forms behave identically; pick whichever reads better in context.

Example of Using the While Loop

Let's consider an example of using the while loop to print the numbers from 1 to 10. Here's the code:

PHP Example of Using the While Loop

php— editable, runs on the server

In this example, we initialize a variable $i to 1 and set the condition to $i <= 10. The loop keeps running while $i is 10 or less. In each iteration, we print the value of $i and increment it by 1. As soon as $i becomes 11, the condition is false and the loop ends. The output of the code is:

1 2 3 4 5 6 7 8 9 10

A practical example: looping over an array

while is often used with a counter to walk through an indexed array. The condition stops the loop at the array's length, so you never read past the end:

<?php

$fruits = ["Apple", "Banana", "Cherry"];
$i = 0;

while ($i < count($fruits)) {
  echo $fruits[$i] . "\n";
  $i++;
}

?>

This prints each fruit on its own line:

Apple
Banana
Cherry

When iterating over an array's elements like this, a foreach loop is usually the cleaner choice — it removes the manual counter entirely. Reach for while when the stopping point depends on something other than a known length (such as data still being available).

Infinite Loops and Breaking Out of Loops

It is possible to create an infinite loop by using an expression that always evaluates to true in the while loop condition. For example:

PHP infinite while loop syntax

while (true) {
  // code to be executed
}

In such cases, it is necessary to use the break statement to exit the loop when a certain condition is met. The break statement will immediately stop the loop and move on to the next statement after the loop. Here's an example:

Infinite Loops and Breaking Out of Loops

php— editable, runs on the server

In this example, the loop prints the numbers from 1 to 10 and then stops. The if test combined with break acts as the real exit condition. The related continue statement is different: instead of leaving the loop, it skips the rest of the current iteration and jumps straight back to the condition check.

while vs. do...while

A while loop checks its condition before running the body, so the body may never run. The do...while loop is its mirror image: it checks the condition after the body, guaranteeing the body runs at least once.

<?php

$i = 100;

// Condition is false from the start — body never runs:
while ($i < 10) {
  echo "while: $i\n";
  $i++;
}

// Body runs once, then the condition is checked:
do {
  echo "do-while: $i\n";
  $i++;
} while ($i < 10);

?>

Output:

do-while: 100

Use do...while when the action must happen before you can know whether to repeat it — for example, prompting for input and only re-prompting if it was invalid.

Conclusion

The while loop runs a block of code as long as its condition stays true, with the condition checked before each iteration. Remember the three keys to using it safely: initialise your control variable before the loop, write a condition that will eventually become false, and update that variable (or break) inside the body. For more loop options, see the overview of PHP loops, the count-controlled for loop, and the comparison operators you'll use to build loop conditions.

Diagram

graph TD;
  A[While Loop Structure] --> B[Expression Evaluation];
  B --> C{Execute Code};
  C --> B;
  B --> D[False];
  D --> E[End Loop];

Practice

Practice
What are the properties of the 'while' loop in PHP?
What are the properties of the 'while' loop in PHP?
Was this page helpful?