while
In PHP, the "while" keyword is used to create a loop that executes a block of code repeatedly as long as a certain condition is true. In this article, we'll
The PHP while Loop
The while loop is the simplest loop in PHP. It runs a block of code over and over as long as a condition stays true, and stops the moment that condition becomes false. Use it when you don't know in advance how many iterations you'll need — for example, reading rows from a database until there are none left, or retrying an operation until it succeeds.
This page covers the syntax of while, the alternative endwhile form, how while differs from do...while, and the most common mistake — the infinite loop — and how to avoid it.
Syntax
while (condition) {
// Code to be executed while the condition is true
}The condition is evaluated before each iteration:
- PHP checks the
condition. - If it is
true, the body runs once, then PHP returns to step 1. - If it is
false, the loop ends and execution continues after the closing brace.
Because the check happens first, the body can run zero times — if the condition is already false on the first check, the loop body is skipped entirely.
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 <?= $i ?></li>
<?php $i++; ?>
<?php endwhile; ?>
</ul>Both forms behave identically; pick whichever reads better in context.
Usage
The "while" keyword is commonly used in PHP programming to create loops that repeat a block of code until a certain condition is met. For example, you might use a "while" loop to iterate over the elements of an array:
Example of PHP while keyword
In this example, the "while" loop is used to iterate over the elements of the $myArray array. The $i variable is used to keep track of the current index in the array, and the loop continues to execute as long as $i is less than the number of elements in the array. Inside the loop, we use the echo statement to output the current element of the array, and then increment $i to move to the next element.
Examples
Here are a few more examples of how the while loop can be used in PHP programming.
Example 1: Generating a random number until a certain condition is met
In this example, we use a "while" loop to generate random numbers until we get a number that is greater than or equal to 10. Inside the loop, we generate a random number using the rand() function and output it using the echo statement. We continue to generate random numbers and output them until we get a number that is greater than or equal to 10.
Example 2: Iterating over a database result set
Iterating over a database result set in PHP
<?php
$query = "SELECT * FROM my_table";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['column_name'] . "<br>";
}In this example, we use a while loop to iterate over the result set returned by a database query. The mysqli_fetch_assoc() function returns the next row as an associative array, or false when there are no more rows — so the loop stops automatically once the result set is exhausted. This works because the assignment $row = mysqli_fetch_assoc($result) evaluates to the assigned value, and false ends the loop.
Avoid the infinite loop
The most common mistake with while is forgetting to change the variable that the condition depends on. If the condition never becomes false, the loop runs forever and freezes your script:
<?php
$i = 0;
// Bug: $i is never incremented, so $i < 5 stays true forever.
while ($i < 5) {
echo "running...\n";
// $i++; <-- this line is missing
}Always make sure something inside the loop moves the condition toward false. You can also exit early with break, or skip the rest of the current iteration with continue.
while vs do...while
A while loop checks the condition before running the body, so the body may execute zero times. A do...while loop checks the condition after the body, so the body always runs at least once:
<?php
$i = 10;
// Condition is false from the start, so this prints nothing.
while ($i < 5) {
echo "while: $i\n";
$i++;
}
// do...while runs the body once before checking, so it prints "do-while: 10".
do {
echo "do-while: $i\n";
$i++;
} while ($i < 5);Reach for do...while when the loop body must run at least once (for example, prompting a user until they give valid input).
Conclusion
The while loop runs a block of code as long as its condition is true, checking that condition before each pass. Use it when the number of iterations isn't known up front, guard against infinite loops by updating the condition inside the body, and choose do...while when the body must run at least once.
To explore other ways to repeat code in PHP, see the for loop (best when you know the iteration count) and the foreach loop (best for walking through arrays). For a high-level comparison of all of them, read the PHP loops overview.