Learn PHP  For Loop 

PHP For Loop  is used to execute the code up to specific number.

If you already know the last value then use it otherwise use while Loop.


<?php

for(initialization; condition; increment/decrement){  
//code execute here 
}  

?>
 

Lets do practice with an example 

<?php  
for($i=1;$i<=5;$i++){  
echo "$i<br/>";  
}  
?>  
 
In above example the code will execute until the value is 5 ( $i = 5 ;)

 1
 2
 3
 4
 5
 
See this so easy right ?

Don't say no . hehe 


Lets do another example  with nested :

In PHP , you can use for loop inside for loop . We call this nested loop .  

 <?php  
for($i=1;$i<=4;$i++) 
{  
    for($b=1;$b<=4;$b++)
       {  
        echo "$i   $b<br/>";  
       }  
}  
?>  
 
Outer Loop Execute 4 Times .
Inside loop execute 4 times inside outer loop . Outer  loop execute 4 times and inner execute 4 times inside outerloop = 4 x 4 =16 

Output :


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

Thats it . :) 

Like use on top right :)



Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :