PHP Looping Statements

In this PHP tutorial, you will learn all about conditional statements in PHP. We will discuss in detail the different types of conditional statements namely for loop, nested for loop, while loop, nested while loop, do…while loop, and foreach loop.

for loop Statement in PHP

The for loop is used to execute the set of the code block a specified number of times. 

Syntax


for(initialization; condition; increment/decrement){  
//code to be executed  
}  
 

There are three parameters in for loop which are:

  • Initialization: We can initialize a counter here. Which can only be used once and it is also an optional parameter
  • Condition: The condition of when to exit the loop will be specified
  • Increment/decrement: It increments or decrements the value of the variable
PHP - For Loop

Example :if Statement in PHP


for ($i = 1; $i <= 10; $i++) {    
echo "$i ";    
}    
 

Output:


1 2 3 4 5 6 7 8 9 10

In the above example, we are printing the numbers from 1 to 10. For that, we first have initialized a variable “i” with value “1” and in the condition parameter we check the condition “i <= 10” and if the condition is true the block of the for loop will execute, and after the execution, the increment/decrement parameter will be executed and in that the value of the variable “i” is increment and the loop will continue till the condition    “i <= 10” becomes false.

What is meant by Nested for loop in PHP?

The nested for loop is meant by when using a for loop inside another for a loop. When the condition of the outer for loop becomes true the inner for loop will be executed. In Nested for loop in every iteration of the outer for loop the inner for loop will execute the entire iteration until the condition of the inner for loop becomes false and it will continue till the condition of the outer for loop becomes FALSE.

Example :if Statement in PHP


for ($i = 1; $i <= 3; $i++) {    
    for ($j = 1; $j <= 5; $j++){
        echo "$i * $j = ". $i * $j. "\n";
    }    
}    
  
 

Output:


1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15

In the above example, we are printing the multiplication of 1, 2, and 3. For this purpose, we are using the nested for loop. Here we first have used a for loop with the parameters “i = 1” as initialization, “i <= 3” as the condition, and “i++” as the increment. Then in the block of the for loop, we have another for loop with parameters “j = 1” as initialization, “j <= 5” as the condition, and “j++”. And in the block of the inner for loop, we are printing the multiplication. We can also see that in the first iteration of the outer for loop the inner for loop will perform the whole iteration until the condition becomes false.  And it continues until the outer for loop condition becomes FALSE.

while loop in PHP

The while loop is used to execute the set of code blocks until the condition specified becomes false. The while loop is also known as the entry control loop because in the while loop the condition is checked first and if the condition is TRUE then the block of the code will be executed otherwise it will exit the loop. 

Syntax


while(condition){  
//code to be executed  
}    
 
PHP - while loop

Example :if Statement in PHP


$i = 1;
while ($i <= 10) {    
echo "$i ";    
$i++;
}    
  
 

Output:


1 2 3 4 5 6 7 8 9 10

In the above example, we are printing the numbers from 1 to 10. For that we first have initialized a variable “i” with value “1” and in the while loop we check the condition “i <= 10” and if the condition is true the block of the while loop will execute in the code block, we are printing the current value of the variable “i” and then increment the value of the variable “i” and again it checks for the condition in while loop and if it is TRUE it will again execute the code block and the loop will continue until the while condition becomes FALSE.

Nested while loop in PHP

The nested while loop is meant by when using a while loop inside another while loop. When the condition of the outer while loop becomes true the inner while loop will be executed. In Nested while loop in every iteration of the outer while loop the inner while loop will execute the entire iteration until the condition of the inner while loop becomes false and it will continue till the condition of the outer while loop becomes FALSE

Example :Nest while in PHP


$i = 1;
while ($i <= 3) {
    $j = 1;
    while ($j <= 5){
        echo "$i * $j = ". $i * $j. "\n";
        $j++;
    }
    $i++;
}    
 

Output:


1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15

In the above example, we are printing the multiplication of 1, 2, and 3. For the purpose now we are using the nested while loop. In here we first have initialized a variable “i = 1” and used a while loop with the condition “i <= 3”. Then in the block of the while loop we have another while loop where we first initialized a variable “j = 1” and we used another while loop with the condition “j <= 5”. And in the block of while loop we are multiplying the values and the printing the result and increment the value of the variable “j” and outside the inner while loop we increment the value of the variable “i” and there will be performed until the condition of the outer while loop becomes FALSE.

do while loop Statement in PHP

The do while loop is mostly similar to the while loop; it will iterate the code block on the basis of the condition. But in the do while loop the code block will be executed at least once. The do while loop is also known as exit control loop because in block loop will first be executed then the condition will be checked.

Syntax


do{  
//code to be executed  
}while(condition);  

 
PHP - while loop

Example :if Statement in PHP


$i = 1;
do{
    echo "$i ";
    $i++;
}while($i <= 10);
  
 

Output:


1 2 3 4 5 6 7 8 9 10

In the above example we are printing the numbers from 1 to 10. For that we first have initialized a variable “i” with value “1” and in the do while loop we first perform the do block where we are printing the current value of the variable “i” and also incrementing the value of the variable “i” then check the condition in while “i <= 10” and if the condition is true then we again execute the do block and the loop will continue until the while condition becomes FALSE.

What is the difference between while loop and do-while loop in PHP?

while loop do while loop
It is also known as entry control loop It is also known as exit control loop
It will first check the condition and if the condition is true the block of the loop will be executed It will first execute the block of the loop then check the condition.
It will only execute the block of code if the condition becomes true It will execute the block of code at least once
It won’t use any semicolon to terminate the loop It uses a semicolon to terminate the loop