PHP Loops
PHPLoops repeat a block of code multiple times. PHP has 4 types of loops:
for— when you know how many times to loopwhile— loops while a condition is trueforeach— loops through arrays (most used!)do...while— runs at least once
Example — PHP
<?php
// For loop
echo "Counting: ";
for ($i = 1; $i <= 5; $i++) {
echo $i . " ";
}
echo "<br><br>";
// Foreach loop (most common)
$fruits = ["Apple", "Banana", "Mango"];
echo "Fruits:<br>";
foreach ($fruits as $fruit) {
echo "🍎 " . $fruit . "<br>";
}
?>
Result
Counting: 1 2 3 4 5
Fruits:
🍎 Apple
🍎 Banana
🍎 Mango
Fruits:
🍎 Apple
🍎 Banana
🍎 Mango