For Loop

For Loop:
for is an iterative statement.it is used to repeat a set of statement numbers of times. the syntax or the method of for loop is:

for(initializations;condition;increment/decrement/updating)
{
-
statements;
-
-
}
       For Loop
For Loop

the final kind of loop statement, the for loop, is also the most powerful, as it combines the abilities to set up variables as you enter the loop, test for the condition while iterating loops and modify variables after each iteration.

1.Example for for loop:
<?php
         $a = 0;
         $b = 0;
         
         for( $i = 0; $i<5; $i++ ) {
            $a += 10;
            $b += 5;
         }
         
         echo ("At the end of the loop a = $a and b = $b" );
      ?>

OUTPUT:
At the end of the loop a = 50 and b = 25
2.Example for for loop:


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

Output:

1
2
3
4
5
6
7
8
9
10


3.Example for foreach loop:

<?php  
$season=array("summer","winter","spring","autumn");  
foreach( $season as $arr ){  
  echo "Season is: $arr<br />";  
}  
?>  

Output:

Season is: summer
Season is: winter
Season is: spring
Season is: autumn
Translate »