PHP  For  Loop statements

PHP for loops are a way of repeating the same block of code over and over.

the PHP for loop can executes a statement or series of statements until a specified condition is false.

syntax:

         for ( initialization  ;  condition  ;  increment/decrement)

                  {
                             the code block to be executed
                  }

initialization

optional.an expression.this expression is executed only once, before the loop is executed.

condition

optional.a boolean expression.if condition is return true, loop body is executed .if condition is false, the loop is terminated.

increment

optional.an expression.the increment expression is executed at the end of every pass through the loop.

example:

  <?php
   
           for( $i=0 ; $i < 10; $i++){
                
                  echo "i value =   $i  <br/>";                                   
             }      
  ?>
  • in the above for loop example -first, the initial value of the variable i is evaluated.
  • then, as long as the value of i is less than 10, the echo statements are executed and i  is    reevaluated.
  • when i is greater than 9, the condition becomes false and control is transferred outside the loop.

the initialization,   condition,   increment statements are optional.so we can write for loop in number of ways.

first form

 <?php    $i = 0 ;     //you can put initialization statement outside for loop parentheses          
                 
       for (   ; $i < 10; $i++){
                
                                   echo  "i value   = $i  <br/>";                                   
                               }  
 ?>

second form

 <?php    $i = 0 ;     //you can put initialization statement outside for loop parentheses          
                 
       for (   ; $i < 10;  ){
                
                                 echo "i value     = $i  <br/>";
           
                                 $i++;  //you can put increment statement inside loop body                      
                           }  
  ?>

PHP infinite for loops

an infinite for loop occurs when a condition always evaluates to true.

here are some pseudocode for an infinite for loop for a web server.

      <?php
      
              for( $i = 0; true ; $i++){     

                               //loop condition always true, so this code represent infinite loop
              
                                           }
                                    
              for( $i = 0; $i < 10; $i--){              
 
                                //i variable  never greater then 10
                    
                                //then loop condition always true, so this code represent infinite loop
                    
                                            }
                                
              for( $i = 0; $i < 10; ){

                               //missing increment statement, so this code represent infinite loop
              
                                         }                               
  ?>

note:if you omit the loop condition, condition always true, in this situation loop never end.so its called infinite loop and this will crash your browser.

to solve this situation, you must provide a break statement inside the loop body.

         for ( ; ; ) {    echo("this is infinite loop") ; break; }

php nested for loops

nesting of loops that is, one for loop statement within another for loop statement.

syntax:

          for( $i=0; $i < 10; $i++){
                                               statement___1;
           
                                               statement___n;                                   
                                     
            for( $j=0; $j < 10; $j++){
                                                                 
                                               statement___n;            
                                           }           
                                     }

example

  <?php
  
          for( $i =1; $i < 4; $i++){    
                           
                       for( $j =1; $j <4; $j++){
                                     
                                                           if( $i == $j){
                                                           
                                                            echo  " i equal jth  $i  ==  $j";
                                                            
                                                            echo  ("<br/>");
                                                            
                                                         }                                                 
                                                    
                                                     }
                                                 
                                                  } 
  ?>