Home »
PHP
PHP Loop statements
By Kongnyu Carine Last updated : November 25, 2023
Where PHP Loops are required?
Imagine that we need a program that says "hello world" 100 times. It's quite stressful and boring to write the statement -- echo "hello world" — 100 times in PHP. This is where loop statement facilitates the work for us.
What is a Loop?
A loop statement is a statement that execute as long as a particular condition is valid and stops when that condition is invalid.
Let's look at the different loops in PHP.
PHP while loop
The while statement executes a particular block of code as long as a statement remains true.
Syntax
Below is the syntax of while loop:
while (condition) {
// code block, if condition is true
}
Example
We want to display the number 1 to 5.
<?php
$x = 1;
while ($x <= 5) {
echo "Number is: $x <br>";
$x++;
}
?>
Output
Number is :1
Number is :2
Number is :3
Number is :4
Number is :5
PHP do...while loop
The do...while loop is the same as the while loop but for that it executes your code atleast once even if the condition is false before checking the condition to true and continues executing as the statement remains true.
Syntax
Below is the syntax of do...while loop:
do {
// code block;
} while (condition);
Example
In this example we will repeat the example above but demonstrate how the do..while loop executes your code atleast once whether true or false before checking the condition.
<?php
$x = 1;
do {
echo "Number is: $x <br>";
$x++;
} while ($x >= 5);
?>
Output
Number is :1
PHP for loop
The for loop works as the while loop but a difference in syntax, in this loop all the things like counter initialization, condition, increment and decrement statements are placed together separated by the semicolon.
Syntax
Below is the syntax of for loop:
for (initialization counter; test counter; increment/decrement counter) {
// code block, if condition is true
}
Here,
- The initialization counter is used to set the initial value.
- Test counter or condition determines the execution process, if true the loop continues if false the loop stops.
- The increment/decrement counter, used to increments or decrements the initial value.
Example
We go with our example again listing numbers from 1 to 5 with the for loop
<?php
for ($x = 1;$x <= 5;$x++) {
echo "Number is: $x <br>";
}
?>
Output
Number is :1
Number is :2
Number is :3
Number is :4
Number is :5
PHP foreach Loop
The PHP foreach loop is used to iterate through an array to access each key/value pair in an array.
Syntax
Below is the syntax of foreach loop:
foreach ($array_variable as $value_variable) {
// code block;
}
Example
In this example, we have an array "cities" with some values. Using the foreach loop, we are accessing each value of the "cities" and printing it.
<?php
$cities = array("New Delhi", "Mumbai", "Banglore");
foreach ($cities as $city) {
echo "$city <br>";
}
?>
Output
New Delhi
Mumbai
Banglore
PHP infinite Loop
To create a never-ending (an infinite) loop, you can use while, do...while, or for a loop by passing true as the condition of the loop.
Infinite while loop
Below is an example of an infinite while loop -
<?php
while(true){
echo "Hello";
}
?>
Infinite do...while loop
Below is an example of an infinite do...while loop -
<?php
do{
echo "Hello";
}while(true);
?>
Infinite for loop
Below is an example of an infinite for loop -
<?php
for(;true;){
echo "Hello";
}
?>