• Struts
  • SQL Tutorial
  • Java IO
  • Servlet
  • jQuery
  • Core Java
  • Spring
  • PHP
  • JavaScript

  • <

    PHP Loop Tutorial

  • while - loops Execute the code as long as the condition is true
  • do...while - loops Execute the code once, and then repeats the loop as long as the condition is true
  • for - loops Execute code at given number of times
  • foreach - loops Execute code for each element in an array


  • PHP While Loop

    while statement will loops through a block of code as long as the condition gets true.

    PHP while Loop Example


    
    <?php 
    $a = 1; 
    
    while($a < 3) {
        echo " number is: $x <br>";
        $x++;
    } 
    ?>
    
    

    Output:
    number is: 1
    number is: 2 
    

    PHP do while Loop



    The do...while loop execute code once and repeat the loop while the given condition is true.


    PHP Do While Example


    <html>
    <body>
    
    <?php 
    $x = 2;
    
    do {
        echo "number is: $x <br>";
        $x++;
    } while ($x <= 5);
    ?>
    
    </body>
    </html>
    

    Output:
    number is: 2 
    number is: 3 
    number is: 4 
    number is: 5 
    


    PHP For Loop



    The For Loop statement is excute given number of tmes.


    PHP For Loop Example


    <html>
       <body>
          
          <?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" );
          ?>
       
       </body>
    </html>
    

    Output:
    At the end of the loop a = 50 and b = 25
    















    © copyright 2017-2021 Completedone pvt ltd.