Loops in PHP (for, while, foreach): A Complete Beginner’s Guide
php foreach loop example with array
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.

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
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 . " ";
}
?>
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;
}
?>
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>";
}
?>
for Loopwhile Loopforeach Loop
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.
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) break;
echo $i;
}
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) continue;
echo $i;
}
Use break when you want to stop the loop early
Use continue when you want to skip certain values but keep looping
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.