The PHP for loop used to execute the code several times during the stack and that is happening according to the dynamic data.

The PHP for loop can be written as the following syntax

for ( expression 1, expression 2, expression 3 )
   => execute the statement

The following list shows you the details of the for loop expression :

  • Expression 1 (Start): This expression would be executed once the for loop starts. That means, the expression 1 is executed for only one time when the loop is created.
  • Expression 2 (Condition): This expression works similar to if condition, so it would return a true boolean value to allow the for loop to continue the execution. Otherwise, it will break the for loop and go out.
  • Expression 3 (Increment or Decrement): This expression designed to be executed at the end of the iteration.

Let’s see an example.

<?php
  for( $i = 0; $i < 5; $i++ ) {
    echo $i;
    echo "\n";
  }
?>

The output would be like the below.

0 1 2 3 4

Analyze the previous example

At the first expression $i = 0; this will be executed for only the first loop. That means it assigns a zero number for $i variable which means, it defined a new variable before start the for loop.

In the second expression, we have a condition. That told the for loop to resume the loop if the value smaller than 5 loops.

But in the last expression, the for loop made an increment for the $i variable. So each time in increase it by 1.

Use Break Statement in PHP For Loop

The break statement tells the current iteration to exit the loop right now. I will use the same previous example, but will tell it to break the loop before the last iteration.

<?php
  for( $i = 0; $i < 5; $i++ ) {
    echo $i;
    echo "\n";
    if ( $i == 3 ) {
      break;
    }
  }
?>

The output would be like the below.

0 1 2 3

In the next section, I will show you how to exclude some loops during the iteration.

Use Continue Statement in PHP For Loop

The continue statement, refers to exclude the current iteration and resume from the following loop. Let’s see an example.

<?php
  for( $i = 0; $i < 5; $i++ ) {
    if ( $i == 3 ) {
      continue;
    }    
    echo $i;
    echo "\n";
  }
?>

The output would be like the following.

0 1 2 4

In the previous example, the continue statement ignored the loop number 4 from the iteration.

Another Written Ways for the PHP For Loop

The PHP for loop should have a known condition to prevent the for loop from the infinite iteration.

PHP for loop with an empty parameters

<?php
  $i = 0;
  for( ; ; ) {
    if ( $i > 10 ) {
      break;
    }
    echo $i;
    $i++;
  }
?>

Another written syntax with embedded HTML or any programming language like javascript.

<?php for( $i = 0; $i < 5; $i++ ): ?>

<b><?php echo $i; ?></b>
<?php endfor;?>

In all previous examples you saw increment examples with PHP for loop. In the next section you will understand how to do decrement.

Decrement in For Loop

In the previous examples we started the loop from the bottom to the top as ascending such as: 0, 1, 2, 3, 4, .. and so one.

But here we need to start the loop from the top to the bottom as descending such as 4, 3, 2, 1, 0. Let’s do that by code.

<?php
  for( $i = 4; $i >= 0; $i-- ) {
    echo $i; 
    echo "\n";
  }
?>

Using For Loop with Arrays

There are several ways to expose the data inside the array. But here I will use the for loop to get each field inside the array.

<?php
  $array = array( 10, 20, 30, 40, 50, 60, 70 );
  for( $i=0; $i < count( $array ); $i++  ) {
    echo $array[$i]; 
    echo "\n";
  }
?>