New: Complete Beginner's Guide to Coding is now available in Premium
Updated: Indian Govt Exam roadmaps now include salary breakdowns & timelines
Tip: Use the Career Hub to explore all career paths in one place
Tutorials PHP PHP Loops

PHP Loops

PHP

Loops repeat a block of code multiple times. PHP has 4 types of loops:

  • for — when you know how many times to loop
  • while — loops while a condition is true
  • foreach — 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