The while loop in PHP allows repeated execution of a code block as long as a given condition remains true. It is especially helpful when the number of iterations is not known in advance.
// Basic structure of a PHP while loop
while (condition) {
// code to be executed
}
// PHP while loop counting from 1 to 5
<?php
$count = 1;
while ($count <= 5) {
echo "Count is: $count<br>";
$count++;
}
?>
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
The loop checks the condition before each iteration. Once $count becomes greater than 5, the condition fails and the loop stops executing.
Adjust the values below to see how the logic gate behaves. This simulator mimics how PHP processes the loop.
Current Value of $i
break to exit the loop early if neededwhile_loop.phpwhile loop to print even numbers from 1 to 20