PHP Loop Statement

Looping is an important topic in PHP programming language. Loop statement is used to iterate a block of statement until the loop condition is false. PHP Loop Statement will be explained in this article from very beginning. But before starting you have to have the basic knowledge on PHP conditional statement. If you are new in PHP programming, feel free to spend some times to study about PHP Conditional Statement and then start reading this article. If you do so, it will be helpful for you to understand PHP Loop Statement clearly.

PHP Loop Statement

PHP Loop Statement

There are four kinds of loop statement in PHP programming language. These are…

  1. for Loop Statement
  2. foreach Loop Statement
  3. while Loop Statement and
  4. do-while Loop Statement

for Loop Statement

PHP Loop Statement works with three parts…

  1. The initialization part to maintain starting point of a Loop Statement.
  2. The condition part to maintain loop life that means how many times the loop will continue.
  3. The increment or decrement part to increase or decrease the starting point.

The structure of for loop statement is given below…

for(initialization; condition; increment or decrement){
// Loop body
}
OR
for(initialization; condition; increment or decrement) :
// Loop body.
endfor;

The for Loop Statement first starts working from the initialization. Then for loop statement checks condition in its parameter. If the condition returns true, the for loop statement increases or decreases the initial parameter and executes its body. If condition returns false, the loop is break and the programming control goes to the next statement.

For example, The below PHP program will show a series numeric value from 1 to 15 using for loop statement.


<?php

for($i=1;$i<=15;$i++){ 
   echo $i." "; 
 } 
?>

Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Here, for Loop Statement first initializes the variable $i equal to 1. Then it checks condition and compares whether $i is less than or equal to 15. If $i is less than or equal to 15, the value of $i is increased by one and prints the value with echo function declared within for loop statement. If $i is greater than 15, the condition returns false and the for loop statement is terminated. So no value greater than 15 is printed by echo function.

The same job can be done by writing echo function 15 times. But think once, how many times have to be spend and how ugly the code is looked. So, PHP Loop Statement makes the code so smart and reduces programmer’s burden.

Nested for loop statement

When for loop statement is used within another for loop statement, then it is known as nested for loop statement. Say, you want to build a pyramid like below.

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

A PHP program to build such a pyramid using nested for loop statement is given below…


<?php

for($i=1;$i<=5;$i++){
for($j=1; $j<=$i;$j++){
     echo $j.' '; 
 } 
    echo ' ';
  } 
?>

while Loop Statement

The operation of while loop statement is same as the for loop statement. But while loop statement has an extra power to work only with condition. That means, the while loop can work without initialization part and increment or decrement part. This type of while loop is mostly used for fetching data from database which known as resourcal data.

The basic structure of a while loop statement is given below.

initialization;
while(condition){
// loop body
increment or decrement statement;
}
OR
initialization;
while (condition):
// loop body
increment or decrement statement;
endwhile;

Say, you want to find the even numbers between 1 to 30. For this, you can use while loop statement to iterate a block of code. PHP program to find even numbers using while loop statement is given below…


<?php

$i=1;
while($i<=30){   
   if($i%2==0){
     echo $i.' '; 
  }
   $i++; 
 }
?>

Output: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30

Here, the body of while loop statement is being executed until $i is greater than 30 and the if conditional statement is checking whether $i is even or odd with modulus operator. As a result, you can see that the even numbers are being printed on the screen.

do-while Loop Statement

The operation of do-while loop is same as while loop. But the only difference is that while loop checks condition first and if the condition is true, the loop body will be executed. On the other hand, do-while loop first executes the body and then checks condition. If the condition is true, the body of do-while loop will be executed again otherwise do-while loop will be terminated.

The basic structure of a do-while loop is given below…

do{
// Loop body
}while (condition);

do-while loop is hardly used in PHP programming. A tiny example of do-while loop is given below…


<?php   
 $i=0;    
  do{
    echo $i; 
  }while($i>0);
?>

Output: 0

The do-while loop will be executed only once. If this program was written within a while loop statement, no output would show because while loop first checked the condition and the condition would be false.

Foreach Loop Statement

foreach loop statement only works on array and objects data type in PHP programming. If you use other data types with foreach loop statement, PHP will show an error message. Foreach Loop Statement is used to iterate in array data and to print its values.

The basic structure of foreach loop statement is given below…

foreach(array_variable as $local_variable){
// foreach loop body
}
And
foreach(array_variable as $index_variable => $local_variable){
// foreach loop body
}

Say, there are five values in an array data. Each time foreach loop will visit to each index in the array data and fetch the array value to the $local_variable. Now you can do anything with this value within the loop body. The second structure will fetch not only the array value but also the array index value at the same time. The array value will be kept into $local_variable and the index value will be kept into the $index_variable. A simple example of foreach loop statement is given below…


<?php 
$data = array(1,2,3,4,5); 
foreach($data as $value){
 echo $value.' '; 
}
?>

Output: 1 2 3 4 5

Another example to show both the array index and the array value from an associative array is given below…


<?php 

$data = array("Apple"=>"Red", "Banana"=>"Yellow", "Mango"=>"Green");

foreach($data as $key => $value){
 echo $key ." is ".$value." ";
}

?>

Output:

Apple is Red

Banana is Yellow

Mango is Green

From the above two simple example, you can easily understand the working mechanism of foreach loop statement. If you have any confusion about array data type and foreach loop statement, spend some times on working with  array in PHP which will help to learn more about array data and foreach loop statement.

PHP Loop Statement has been explained elaborately in this article. If you have any question about PHP loop statement, feel free to contact with me from Contact us page. I’ll try my best to stay with you. PHP break and continue statement used in loop statement will be explained in next article. Spend some times to study that article for doing advance operation in PHP loop statement.

No Responses

Leave a Comment

Your email address will not be published. Required fields are marked *