for-loop

Last updated on

The PHP for loop executes the code multiple times based on dynamic data within the stack.

The PHP for loop can be written as the following syntax.

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

The list below provides a breakdown of the components of a for loop expression:

  • Expression 1 (Start): This expression is executed only once when the loop starts.
  • Expression 2 (Condition): This expression functions similarly to an “if” statement and returns a boolean value to determine whether the loop should continue or break.
  • Expression 3 (Increment or Decrement): This expression is executed at the end of each iteration of the loop.

Let’s see an example.

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

The following output would be similar to the one shown below.

0 1 2 3 4

The initial statement “$i = 0;” only executes once during the first loop and sets the value of the $i variable to zero, effectively defining a new variable before the loop begins.

The second expression sets a condition that instructs the for loop to continue looping as long as the value of $i is less than 5.

In the final expression, the for loop increments the value of $i by 1 with each iteration.

Use Break Statement in PHP For Loop

Using the break statement causes the current iteration to immediately exit the loop. To illustrate this, I will use the same example as before, but will instruct the program to break the loop before the final iteration.

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

The result will appear as shown below.

0 1 2 3

In the following segment, I will demonstrate how to skip certain loops while iterating.

Use Continue Statement in PHP For Loop

The continue statement allows you to skip the current loop and continue with the next one. An example will help illustrate this concept.

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

The following would be the resulting output.

0 1 2 4

In the example preceding this one, the continue statement excluded the fourth iteration of the loop.

Another Written Ways for the PHP For Loop

To avoid infinite iteration in the PHP for loop, developers must provide a known condition. The PHP for loop must not have 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;?>

Another possible syntax is to include HTML or another programming language such as JavaScript within the written code.

Decrement in For Loop

In the previous examples, we initiated the loop in ascending order, starting from the bottom such as 0, 1, 2, 3, 4, and so on. However, in this case, we must start the loop in descending order, from the top such as 4, 3, 2, 1, 0. Let’s achieve this by writing the necessary code.

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

Using For Loop with Arrays

There are various methods to extract the data from an array, but in this case, I will utilize a for loop to retrieve each element within the array.

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

Wrapping Up

The PHP for loop allows for repeated execution of code based on a specified condition. It can be used in various scenarios and can automate repetitive tasks by utilizing its features such as initialization, condition, and increment expression.

Share on: