PHP Loop Statements Explanation

Loop statements are important topics in PHP programming language. Like other programming languages PHP loop statements are frequently used in PHP programming.So, those who want to be expert on PHP programming should be expert enough on PHP loop statements. PHP loop statement is used to iterate the body of loop statement until the condition of the loop statement becomes false. In my previous article I explained PHP conditional statements and in this article I will discuss PHP loop statements elaborately.

PHP Loop Statement

Looping Statements in PHP

Various types of loop statements are used in PHP programming for different purposes. There are four types of looping statements in PHP.

  • PHP for Loop
  • PHP while Loop
  • PHP do while Loop and
  • PHP foreach Loop

Every PHP loop statement has three parts. The following listing is a summary of these three parts.

  •  Initialization part: It maintains the starting point of a loop statement.
  • Condition part: It maintains the life of a loop statement. That means, the condition part takes decision how many times the loop will be continue.
  • Increment or Decrement part: The increment or decrement part increases or decreases the starting point.

PHP for loop

PHP for loop is a frequently used looping statement in PHP programming. So, the structure of for loop statement should be remember and understand clearly. The PHP for loop statement is expressed like the following structure.

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

How PHP for Loop Works

PHP for loop statement starts working from the initialization. It first checks the initial value and then checks condition. If the condition returns true, for loop statement executes its body and increases or decreases the initial value according to the defined option and then it checks the condition again. If the condition return true, it does the previous step again until the condition returns false. If the condition returns false, the loop breaks and the programming control goes ahead.

PHP for Loop Example

The following PHP program is a simple example of PHP for loop statement that will show a series of numeric value from 1 to 15.

<?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

PHP for Loop Example Explanation

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

Nested for Loop Statement

A for loop statementcan be declared within another for loop statement. If a for loop is declared within another for loop, it is called nested for loop statement. For example, we want to build a pyramid like the following expression.

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

To build the above pyramid, we can use PHP for loop statement. The following PHP program is an example of PHP nested for loop statement that results the above pyramid.

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

PHP while Loop

The operation of PHP while loop statement is same as the for loop statement. That means, it works with initialization part, conditional part and the increase or decrease part like the for loop statement. But PHP while loop statement has an extra power to work with only the conditional part. That means, the while loop can work without initialization part and increment or decrement part. This type of while loop is mostly used to fetch data from database which known as resourcal data. The while loop is also used to do PHP file operation.

The while loop statement is expressed like the following structure.

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

PHP while Loop Example

Say, we want to find the even numbers between 1 to 30. For this mathematical operation we can use while loop statement. The following PHP program is an example of PHP while loop statement to find even numbers between 1 to 30.

<?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, we can see that the even numbers are being printed on the screen.

PHP do while Loop

The operation ofdo while loop is same as the while loop statement. But the only difference is 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 following expression is the structure of PHP do while loop statement.

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

PHP do while Loop Example

The do-while loop is hardly used in PHP programming. The following PHP program is an example of PHP do while loop statement.

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

Output

0

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

PHP foreach Loop

PHP foreach loop statement only works on array and objects data in PHP programming. If we use other data with foreach loop statement, PHP will show an error message. The foreach loop statement is used to iterate in array data and to print its values.

The PHP foreach loop statement is expressed like the following structure.

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 and we want to retrieve the value of that array with foreach loop. Then, each time foreach loop will visit the index of the array until the array index becomes NULL and fetch the array value into the $local_variable.  Now we 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 the $local_variable and the index value will be kept into the $index_variable.

PHP foreach Loop Example

The following PHP program is an example of PHP foreach loop statement that will show the value of an array.

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

Output

1 2 3 4 5

The following example is another example of PHP foreach loop statement where both the array index and the array value have been showing from an associative array.

<?php 
$data = array("Apple"=>"Red", "Banana"=>"Yellow", "Mango"=>"Green");
 foreach($data as $key => $value){
  echo $key ." is ".$value."<br/>";
 }
?>

Output:

Apple is Red
Banana is Yellow
Mango is Green

PHP Loop Statements have been explained elaborately in this article. I hope you will now be able to apply PHP loop statements in PHP programming properly. However, if you face any confusion to understand PHP loop statements, feel free to discuss in comment or contact me from Contact page. I will try my best to stay with you.

Related post:

php-loop-statement-explanation

ABU SAYEED

I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Web development is also my favorite topic and love to work with PHP, HTML, CSS, JavaScript, AJAX, jQuery, WordPress and CodeIgniter framework. Follow Me: Facebook, Twitter and Linkedin.

Your name can also be listed here. Have an IT topic? Submit it here to become a iTechSheet author.

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.