The PHP loop runs tasks without the need to write the same lines again.
It lets you handle data step by step.
Table of Content
Understand the Loop in PHP
A loop in PHP runs a block of code many times. You use loops when you want to do the same task for each item in a list or repeat steps until a rule is true. Loops save time and space in code.
You donāt need to repeat a task ten times. Write it once and run it multiple times with a loop.
Loops can run a fixed number of times or until a condition changes. For example, you might loop through users in a database or count from 1 to 10.
Here are the types of loops in PHP:
- for loop
- while loop
- do…while loop
- foreach loop
So, how does the loop work in PHP?
A loop runs a block of code over and over. It checks a condition each time. The loop runs again when the condition is true. When the condition is false, the loop stops.
This design gives you control over how many times the code runs. You can use variables to count or move through arrays.
You should avoid loops when you can use built-in functions such as array_map or whatever. Loops can slow your code if you do not manage them well.
Avoid loops with conditions that never change, or you will create infinite loops. Do not use loops for simple tasks that do not need repetition.
Types of Loops in PHP
The for Loop:
The for loop executes a block of code a specific number of times. It uses three parts:
- Start value.
- Test condition.
- Increment or decrement.
Here is an example:
for ($i = 1; $i <= 5; $i++) {
echo $i . " ";
}
It prints:
1 2 3 4 5
This loop starts at 1. It runs while $i
is 5 or less. It adds 1 each time.
The while Loop:
The while loop runs while a condition remains true. It checks the condition before each run. The loop stops when the condition is false.
For example:
$i = 1;
while ($i <= 3) {
echo $i . " ";
$i++;
}
It runs until $i
is greater than 3.
The doā¦while Loop:
The doā¦while loop runs the block once first. After that, it checks the condition. It repeats the block if the condition is true.
For example:
$i = 1;
do {
echo $i . " ";
$i++;
} while ($i <= 2);
It prints:
1 2
This ensures the code runs at least once.
The foreach Loop:
The foreach loop works with arrays. It runs once for each item. For example:
$colors = ['red', 'blue', 'green'];
foreach ($colors as $color) {
echo $color . " ";
}
The output:
red blue green
It shows you all items in an array.
You can stop a loop early with the break statement. For example:
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
break;
}
echo $i . " ";
}
It prints:
1 2
It stops when $i
is 3.
Nested Loops in PHP
Nested loops are loops inside other loops. You use them for grids, tables, or multi-level data.
Here is an example that prints a number grid:
for ($i = 1; $i <= 2; $i++) {
for ($j = 1; $j <= 3; $j++) {
echo "$i,$j ";
}
}
Here is the output:
1,1 1,2 1,3 2,1 2,2 2,3
The outer loop runs twice. The inner loop runs three times for each outer loop.
Another example of a star pattern:
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo "*";
}
echo "\n";
}
Output:
*
**
***
Each row has one more star.
Table with data:
$rows = 2;
$cols = 2;
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $cols; $j++) {
echo "Cell($i,$j) ";
}
echo "\n";
}
Output:
Cell(1,1) Cell(1,2)
Cell(2,1) Cell(2,2)
This creates a simple table with labeled cells.
Loop Control with Conditional Statements
Conditional statements guide what happens in a loop. You can use if
, break
, and continue
inside loops to control flow.
Example 1: Skip values:
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo $i . " ";
}
Output:
1 2 4 5
The continue
skips the code for 3.
Example 2: Stop on condition:
$numbers = [2, 4, 6, 8, 10];
foreach ($numbers as $num) {
if ($num > 6) {
break;
}
echo $num . " ";
}
Here is the output:
2 4 6
The break statement stops when the number is greater than 6.
Examples 3: Even numbers only:
$i = 1;
while ($i <= 6) {
if ($i % 2 != 0) {
$i++;
continue;
}
echo $i . " ";
$i++;
}
The output:
2 4 6
This prints even numbers only.
Examples of PHP Loops
Count from 1 to 3:
for ($i = 1; $i <= 3; $i++) {
echo $i . " ";
}
The output:
1 2 3
This for
loop starts with $i = 1
and runs while $i
is less than or equal to 3. In each step, it prints the value of $i
with a space after it and adds 1 to $i
after every iteration, so the output shows 1 2 3
.
List array items:
$fruits = ['apple', 'banana'];
foreach ($fruits as $fruit) {
echo $fruit . " ";
}
The output:
apple banana
This code creates an array called $fruits
with apple
and banana
. The foreach
loop goes through each item and puts it in $fruit
. It prints each fruit followed by a space, so the output is apple banana
.
While loop countdown:
$i = 3;
while ($i > 0) {
echo $i . " ";
$i--;
}
The output:
3 2 1
This code sets $i
to 3. The while
loop runs as long as $i
is more than 0. It prints the value and then subtracts 1, so the output is 3 2 1
.
Nested number grid:
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
echo "$i$j ";
}
}
Output:
11 12 13 21 22 23 31 32 33
This code has two nested for
loops. The outer loop runs $i
from 1 to 3, and the inner loop runs $j
from 1 to 3 for each $i
. It prints each pair as ij
Filter even numbers in the array:
$nums = [1, 2, 3, 4, 5, 6];
foreach ($nums as $num) {
if ($num % 2 != 0) {
continue;
}
echo $num . " ";
}
Output:
2 4 6
This code creates an array with numbers 1 to 6. The foreach
loop checks each number and continue
skips odd ones. It prints only even numbers
Wrapping Up
In this article, you learned many topics, such as
- What a loop in PHP is
- How it works, when to avoid it
- Types of loops
- Nested loops
- How to control loops with conditions.
Here is a quick recap:
- Loops repeat tasks without extra code.
- Types include for, while, do…while, and foreach.
- You can use break and continue to control loops.
- Nested loops help with grids and tables.