Loops Examples Using PHP

admin_img Posted By Bajarangi soft , Posted On 02-02-2024

In PHP we use loops for execute the same block of code for use again and again until the condition is true. loops can save your time and efforts also.

Types of loop in PHP

Differnt Types Of Loops In PHP

  • In PHP we use loops for execute the same block of code for use again and again until the condition is true. loops can save your time and efforts also.
  • Instead of adding several almost equal code-lines in a script it,s easy to use loops. 
  • PHP supports four different types of loops as given below,
  1. While loops through a block of code as long as the condition specified evaluates to true.
  2. do…while loops through a block of code once, and then repeats the loop as long as the specified condition is true.
  3. for PHP for loop can be used to traverse set of code for the specified number of times.
  4. foreach loops through a block of code for each element in an array.

 PHP while Loop

The While statement will loops through a block of code as long as the condition specified in the While statement evaluate to true.
 
while(condition) {
    // write your code here
}

While Loop Flowchart :



Example :
 
<?php    
$b = 1;    
while($b<=10) {    
echo "$b<br/>";    
$b++;    
}    
?> 

Output :
1
2
3
4
5
6
7
8
9
10

You can also print alphabets using while loop.

 PHP Do...while Loop

The do-while loop is a variant of while loop, which evaluates the condition at the end of each loop iteration.
in a simple way the do...while statement will execute a block of code at least once it then will repeat the loops as long as long as a condition is true.

do {
    // your code goes here
}
while(condition);
 

Do...while loop Flowchart :

Example : 

here, we have one variable called $n then we print that  variable and apply increment operator on it . Now while loop run until variable value bacame less than 10 or equal to it.
 
<?php    
$n=1;    
  do {    
  echo "$n<br/>";     
  $n++;    
  } while($n<=10);    
?> 

Output :

1
2
3
4
5
6
7
8
9
10

 PHP For Loop

 When you properly know that how many time you want to run your statement then we use for loop.
 
for (initialization; condition; increment) {
    // Code to be executed
}
 
here, initializer used to set the start value of counter of the number of loop iterations.

Example :

<?php  
for ($x = 1; $x <= 10; $x++) {
  echo " $x <br>";
}
?> 

Output :

1
2
3
4
5
6
7
8
9
10

PHP for...each Loop

For...each loop only works on array. 
to use key and value from array we use this loop in array.

 
foreach ($array as $value) {
   your code goes here
}

 as shown in syntax array In every loop the value of the current element of the array is assigned to $value and the internal array pointer is advanced by one and the process   continue to reach the last array element.

There is one more syntax for foreach loop
 
foreach ($array as $key => $element) {   
   your code goes here
}  

$array is an array. In every loop the current element's key is assigned to $key and the internal array pointer is advanced by one and the process continue to reach the last array element.

Foreach Loop Flowchart :

 
 

Example : 1

<?php
         $array = array( 1, 2, 3, 4, 5);
         foreach( $array as $value )
         {
         echo " $value <br />";
        }
?>

Output :

1
2
3
4
5

Example : 2

  <?php
    $comics = array(
    "Spiderman" => "Peter Parker",
    "Ironman" => "Tony Stark",
    "Wolverine" => "Logan"
);
 
foreach($comics as $key => $value){
    echo $key . " : " . $value . "<br>";
}
?>

Output :

Spiderman : Peter Parker
Ironman : Tony Stark
Wolverine : Logan

Related Post