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

11/19/2025

php foreach loop example with array

Go Back

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.

In PHP, loops are widely used when working with data such as arrays, database results, or repetitive operations like printing values or performing calculations. They play a crucial role in reducing code duplication and improving readability.

Overall, loops are a powerful feature that helps developers write cleaner, more efficient, and scalable code in PHP applications.

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.


php foreach loop example with array

What Are Loops in PHP?

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

Loops are an essential concept in PHP that allow developers to execute a block of code repeatedly based on a given condition. Instead of writing the same code multiple times, loops help automate repetitive tasks, making programs more efficient and easier to manage.

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

for Loop

  • Used when the number of iterations is known
  • Best for counter-based loops
  • Initialization, condition, and increment are in one line
    Example Use Case: Print numbers from 1 to 100

while Loop

  • Used when iterations depend on a condition
  • Runs until the condition becomes false
  • Useful when the number of iterations is not fixed
    Example Use Case: Reduce balance until it becomes 0

foreach Loop

  • Specifically used for arrays and collections
  • Automatically iterates through each element
  • No need for counters or conditions
    Example Use Case: Display list of users or items

 

 


Break and Continue in Loops

In PHP, controlling how a loop behaves is just as important as creating the loop itself. This is where the break and continue statements come into play. They provide developers with more flexibility to manage loop execution based on specific conditions.

The break statement is used to immediately terminate a loop when a certain condition is met, while the continue statement is used to skip the current iteration and move to the next one. These control statements are especially useful when working with large datasets or complex conditions, helping improve both performance and code readability.

Overall, break and continue enhance loop control, allowing developers to write more efficient and logically structured programs.

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;
}

When to Use

  • Use break when you want to stop the loop early

  • Use continue when you want to skip certain values but keep looping


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.