Loops in PHP (for, while, foreach): A Complete Beginner’s Guide

11/19/2025
All Articles

php foreach loop

Loops in PHP (for, while, foreach): A Complete Beginner’s Guide

Loops in PHP (for, while, foreach): A Complete Beginner’s Guide

Loops are essential in PHP programming because they allow you to execute a block of code repeatedly. Whether you're processing arrays, generating dynamic content, or performing calculations, loops save time and make your code efficient.

This guide covers the three most commonly used loops in PHP:

  • for loop

  • while loop

  • foreach loop

Each loop is explained with syntax, examples, use cases, and best practices.


⭐ What Are Loops in PHP?

A loop allows you to run the same piece of code multiple times until a condition is met.

You use loops when:

  • You need repetition

  • You want to iterate through lists or arrays

  • You want to reduce code duplication


1. for Loop in PHP

The for loop is used when you know exactly how many times you want to run the code.


Syntax of for loop

for (initialization; condition; increment) {
    // Code to execute
}

Explanation:

  • initialization → starting point

  • condition → loop runs while this is true

  • increment → updates the counter after each iteration


Example 1: Print numbers from 1 to 5

<?php
for ($i = 1; $i <= 5; $i++) {
    echo $i . "<br>";
}
?>

Example 2: Loop through even numbers

<?php
for ($i = 2; $i <= 10; $i += 2) {
    echo $i . " ";
}
?>

2. while Loop in PHP

Use the while loop when you do NOT know the number of iterations in advance. The loop continues as long as the condition is true.


Syntax of while loop

while (condition) {
    // Code to execute
}

Example 1: Simple while loop

<?php
$count = 1;

while ($count <= 5) {
    echo "Count: $count <br>";
    $count++;
}
?>

Example 2: Loop until user balance reaches zero

<?php
$balance = 100;

while ($balance > 0) {
    echo "Balance: $balance <br>";
    $balance -= 20;
}
?>

3. foreach Loop in PHP

The foreach loop is specifically designed for arrays. It loops through each item in the array without needing a counter.


Syntax of foreach loop

foreach ($array as $value) {
    // Code using $value
}

Or with keys:

foreach ($array as $key => $value) {
    // Code using $key and $value
}

Example 1: Loop through an array

<?php
$colors = ["Red", "Green", "Blue"];

foreach ($colors as $color) {
    echo $color . "<br>";
}
?>

Example 2: Loop with key and value

<?php
$ages = [
    "Shubham" => 25,
    "Amit" => 30,
    "Ritu" => 28
];

foreach ($ages as $name => $age) {
    echo "$name is $age years old.<br>";
}
?>

Comparison of Loops in PHP

Loop Best Used When Example Use Case
for You know the number of iterations Print 1 to 100
while Iterations depend on condition Reduce balance until 0
foreach Iterating through arrays Display users or items

Break and Continue in Loops

break → stops the loop immediately

for ($i = 1; $i <= 10; $i++) {
    if ($i == 5) break;
    echo $i;
}

continue → skips current iteration

for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) continue;
    echo $i;
}

Common Mistakes Beginners Make

  • Forgetting to update the counter (infinite loop)
  • Using foreach for non-array variables
  • Missing curly braces
  • Using while when for is easier to understand

Conclusion

Loops are the foundation of automation in PHP. Understanding for, while, and foreach loops will help you build efficient applications, process arrays, and handle repeated tasks with ease.

Practice each loop with different examples to fully master control flow in PHP.

Article