Loops execute the same block of code repeatedly. The while and do…while loops run as long as a condition is true.
$i = 1;
while ($i <= 5) {
echo "Number: $i <br>";
$i++;
}
Executes the block at least once before checking the condition.
$i = 1;
do {
echo "Number: $i <br>";
$i++;
} while ($i <= 5);
$i = 0;
while ($i < 10) {
$i++;
if ($i % 2 === 0) continue; // skip even numbers
if ($i === 7) break; // stop at 7
echo $i . " ";
}
// Output: 1 3 5
$handle = fopen("data.txt", "r");
while (!feof($handle)) {
$line = fgets($handle);
echo $line . "<br>";
}
fclose($handle);