PHP Loops

PHP Loops

Loops are used to do some task or execute some statements or block of code number of times as specified


PHP has 4 different types of loops

  • For Loop
  • While Loop
  • DoWhile Loop
  • Foreach Loop


  • For Loop

    For loop is used where when we know the number of time we want to execute the code.


    Syntax


    for (initialization; condition; increment)
    {
      code to be executed;
    }
    


    Code Explanation

  • Initializing value is the starting value of the counter
  • Condition is the conditional used to stop the loop execution
  • Increment is the increment of the initializing value

  • Example of For Loop

    <?php
    $a = 0;
    $b = 0;
    
    for( $i=0; $i<5; $i++ )
    {
        $a ++;
        $b ++;
    }
    echo ("At the end of the loop a=5 and b=5" );
    ?>
    



    While Loop

    While loop is used where we want to execute the code as long as the condition is true.


    Syntax


    while (condition)
    {
        code to be executed;
    }
    


    Code Explanation

  • Condition is the conditional used to stop the loop execution

  • Example of While Loop

    <?php
    $a = 0;
    $b = 0;
    
    while( $i < 5)
    {
        $a ++;
        $b ++;
    }
    echo ("At the end of the loop a=5 and b=5" );
    ?>
    



    Do While Loop

    Do While loop is used where we want to execute the code at least one time and then it repeats the loop as long as the condition remain true.


    Syntax


    do
    {
        code to be executed;
    }
    while(condition)
    


    Code Explanation

  • Condition is the conditional used to stop the loop execution

  • Example of Do While Loop

    <?php
    $a = 0;
    $b = 0;
    
    do
    {
        $a ++;
        $b ++;
    }
    while( $i < 5)
    
    echo ("At the end of the loop a=5 and b=5" );
    ?>
    



    For each Loop

    For each loop is used to do looping with arrays it loop through each key/value pair in an array.


    Syntax


    foreach(array as value)
    {
        code to be executed;
    }
    


    Code Explanation

  • Condition is the conditional used to stop the loop execution

  • Example of For each Loop

    <?php
    
    $array = array( 1, 2, 3, 4, 5);
    
    foreach( $array as $value )
    {
      echo "Value is ".$value;
    }
    
    ?>
    


    Continue Statements

    Continue is used to stop the current operation of a loop when the condition satisfy but it does not terminate the loop


    for( $i=0; $i<10; $i++ )
    {
      if( $i == 5 )continue;
      echo "Value is ".$i;
    }
    

    This will display 1 to 10 execpt 5 because the condition statify on $i=5



    Break Statements

    Break is used to stop the loop when the condition satisfy.


    for( $i=0; $i<10; $i++ )
    {
      echo "Value is ".$i;
      if( $i == 5 )break;
    }
    

    This will display 1 to 5 because the condition statify on $i=5 and the loop terminates

    PrevNext

    Join Us And Get Our Latest Tutorials Updates Via Email