your image

PHP For Loop - javatpoint

javatpoint
Related Topic
:- PHP Java computer programming

PHP For Loop

PHP for loop can be used to traverse set of code for the specified number of times.

It should be used if the number of iterations is known otherwise use while loop. This means for loop is used when you already know how many times you want to execute a block of code.

It allows users to put all the loop related statements in one place. See in the syntax given below:

Syntax

  1. for(initialization; condition; increment/decrement){  
  2. //code to be executed  
  3. }  

Parameters

The php for loop is similar to the java/C/C++ for loop. The parameters of for loop have the following meanings:

 

 

OOPs Concepts in Java

initialization - Initialize the loop counter value. The initial value of the for loop is done only once. This parameter is optional.

condition - Evaluate each iteration value. The loop continuously executes until the condition is false. If TRUE, the loop execution continues, otherwise the execution of the loop ends.

Increment/decrement - It increments or decrements the value of the variable.

Flowchart

Example

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

Output:

12345678910

Example

All three parameters are optional, but semicolon (;) is must to pass in for loop. If we don't pass parameters, it will execute infinite.

  1. <?php  
  2.     $i = 1;  
  3.     //infinite loop  
  4.     for (;;) {  
  5.         echo $i++;  
  6.         echo "</br>";  
  7.     }  
  8. ?>  

Output:

1234...

Example

Below is the example of printing numbers from 1 to 9 in four different ways using for loop.

  1. <?php  
  2.     /* example 1 */  
  3.   
  4.     for ($i = 1; $i <= 9; $i++) {  
  5.     echo $i;  
  6.     }  
  7.     echo "</br>";  
  8.       
  9.     /* example 2 */  
  10.   
  11.     for ($i = 1; ; $i++) {  
  12.         if ($i > 9) {  
  13.             break;  
  14.         }  
  15.         echo $i;  
  16.     }  
  17.     echo "</br>";  
  18.       
  19.     /* example 3 */  
  20.   
  21.     $i = 1;  
  22.     for (; ; ) {  
  23.         if ($i > 9) {  
  24.             break;  
  25.         }  
  26.         echo $i;  
  27.         $i++;  
  28.     }  
  29.     echo "</br>";  
  30.       
  31.     /* example 4 */  
  32.   
  33.     for ($i = 1, $j = 0; $i <= 9; $j += $i, print $i, $i++);  
  34. ?>  

Output:

 

123456789123456789123456789123456789

PHP Nested For Loop

We can use for loop inside for loop in PHP, it is known as nested for loop. The inner for loop executes only when the outer for loop condition is found true.

In case of inner or nested for loop, nested for loop is executed fully for one outer for loop. If outer for loop is to be executed for 3 times and inner for loop for 3 times, inner for loop will be executed 9 times (3 times for 1st outer loop, 3 times for 2nd outer loop and 3 times for 3rd outer loop).

Example

  1. <?php    
  2. for($i=1;$i<=3;$i++){    
  3. for($j=1;$j<=3;$j++){    
  4. echo "$i   $j<br/>";    
  5. }    
  6. }    
  7. ?>  

Output:

1 11 21 32 12 22 33 13 23 3

PHP For Each Loop

PHP for each loop is used to traverse array elements.

 

Syntax

  1. foreach( $array as $var ){  
  2.  //code to be executed  
  3. }  
  4. ?>  

Example

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

Output:

 

Season is: summerSeason is: winterSeason is: springSeason is: autumn

Comments