PHP While Loop

php while loops can executes a statement or series of statements until a specified condition is false. php while and do-while loops are a way of repeating the same block of code over and over.


PHP :- Loops

PHP provide the four different type of loop statements:

  • while - loop statement.
  • do  . . . while loop statement
  • for - loops through a block of code a specified number of times.
  • foreach - loops provides an easy way to iterate over arrays.

PHP while loop

the while loop can executes a statement or series of statements until a specified condition is false. the while statement is normally used when the number of iterations is not known.

the while loop condition is evaluated before executing the loop body and the loop body statements executes as long as the loop condition is true.

the while is an entry-controlled loop statement.

syntax:

         while(condition){
 
                                 statement____1;
                               
                                 statement____2;
                               
                                 statement____n;
                            }

how it work :

the condition test occurs before statement in the loop are executed.if the condition returns true,then the body of the loop is executed.after execution of the body, the condition is once again evaluated and if it is true, the body is executed once again.this process of repeated execution of the body continues until the condition finally becomes false and the control is transferred out of the loop.

example:

the loop in this example will continues to run as long as the variable i is less than 10:

    <?php
        
            $i  = 0 ;      // condition control variable
                             
            while( $i < 5 ){
                         
                                  echo   "the while condition return true  = $i  <br/>";
                                         
                                          ++$i;     // i variable increment 
                              }
                                              
                                  echo   "the while condition return false" ;
     ?>

PHP do while loop

The php do while loop is a variant of the while loop. This loop will execute the code block once before checking condition, if the condition is true, then it will repeat the loop as long as the condition is true.

the while statement must end with semicolon(;)

syntax:

          do
              {
                  statement___1;
           
                  statement___2;
           
                  statement___n;
            
            }while ( condition );

the phpdo while construct provides an exit-controlled loop and therefore the body of the loop is always executed at least once.

   <?php     $i = 0 ;
             
        do{
                   echo "do loop statement block always execute at least once " ;
                                     
                   echo "<br/>i value = $i";
                   
                   ++$i;  // i value 1
                               
          }while( $i < 1);     // condition always return false
                               
     ?>

note : do not forget to increase the variable used in the condition, otherwise the loop will never end.


nested while loops in php

nesting of loops, that is, one while statement within another while statement.

syntax:

          do
              {
                  statement___1;
           
                  statement___n;                                   
                                     
                                        do
                                            {
                                                                 
                                               statement___n;
            
                                           }while( condition );
            
            }while ( condition );

example

    <?php
  
             $i = 0 ;
           
          do{    
                        $j = 0 ;
          
                do{
                       if( $i === $j ){
                         
                    echo ("<br/>"."the i and jth value is equal   $i       $j");
                                     
                                 }
                      
                        ++$j;
                                 
                    }while( $j<5 );
          
           ++$i;
                    
         }while( $i<5 );
         
     ?>

PHP infinite while loops

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

an infinite loop means the loop condition will never false and therefore never exit.

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

                       while( true )   {  //read request    }

 if you forget to increase the variable used in the condition, the loop will never end.

         $i=0;       while( $i < 5 )  {    --$i ;  }

note : the infinite loop crash your browser so its never used in programming.

      <?php
      
              while(true){   /* this code represent infinite  while loop */ }
              
              $i=0;
              
              while( $i < 5 ){  
                                  /* this code represent infinite while loop
                                 
                                   because you forget to increase the i variable */ }
              
              while( $i < 5 ){  --$i; 
              
                                  /* this code represent infinite loop 
                                   
                           because you decrease i variable so condition never false*/}
              
              while( 1 ){  /* this code represent infinite loop  */
                
                              [ 1 for true 
                            
                                0 for false  ]   }
                                
              while(true){ echo "infinite loop <br/>" break;
              
 /*if you add 'break' to a loop, the loop will end, no matter whether the 
   
    condition is false or not */       }