PHP for Loop

The for loop in PHP, is used when we need to execute some block of code, multiple times. For example:

<?php
   for($x=1; $x<=10; $x++)
      echo $x, "<BR>";
?>

The output is:

php for loop

The number of times, the block of code inside the loop, will be executed, depending upon, how many times the condition of the loop, evaluates to be true. In above example, since the condition $x<=10; evaluates to be true ten times, therefore the statement echo $x, "<BR>"; has been executed for ten times.

The above program can also be written as:

<?php
   for($x=1; $x<=10; $x++)
   {
      echo $x;
      echo "<BR>";
   }
?>

PHP for Loop Syntax

The syntax of for loop in PHP, is:

for(initialize; condition; update)
{
   block of code;
}

The execution of the for loop starts from initialize. This statement executed at start of the loop, and only executes at once. This is used to initialized the loop variable, for example: $x=1;

Before entering into the body of the loop, the condition; expression must be evaluated to be true.

If the condition; evaluates to be true, then block of code; will be executed.

After executing the block of code;, the program flow goes to update part of the loop, to update the loop variable.

After updating the loop variable, the condition; gets evaluated, as already told, before entering into the loop, the condition; expression must be evaluated to be true.

This process continues, until the condition; evaluates to be false.

PHP for Loop Example

Now let me create an example of for loop along with its output and in-depth description:

<?php
   for($num=3; $num<=30; $num=$num+3)
   {
      echo $num;
      echo "<BR>";
   }
?>

The output produced by above PHP example on for loop, is shown in the snapshot given below:

for loop in php

The dry run of above example is:

  • The value 3 has been initialized to the variable $num, as initially the first statement (initialize) always gets executed first and only for once
  • Now since the condition $num<=30 or 3<=30 evaluates to be true
  • Therefore, program flow goes inside the loop
  • And using the two echo statement, first the value of $num gets printed, then a line break will be inserted using the BR tag
  • Now the program flow goes to the update part, and will execute the statement available there, that is $num=$num+3
  • Using the update statement, the value of $num will be incremented by 3. Therefore, now $num=6
  • Again the condition $num<=30 will be evaluated with new value of $num
  • That is, the condition $num<=30 or 6<=30 evaluates to be true again
  • Therefore program flow again goes inside the loop
  • And the value of $num that is 6, will be printed on the output, followed by a new line or line break
  • Again the value of $num will be incremented by 3, using the update statement. Now $num=9
  • The condition $num<=30 or 9<=30 evaluates to be true at third time
  • Therefore program flow again goes inside the loop for third time
  • This process continues, until the condition evaluates to be false

You can also use multiple initialize, update, and/or condition statements. For example, in the following example, I'm going to use two initialization codes for the initialize part of the loop:

<?php
   for($num=4, $i=1; $i<=10; $i++)
      echo "$num * $i = ", $num*$i, "<BR>";
?>

The output of this example, is shown in the snapshot given below:

for loop example in php

Note - On using multiple conditional statements for condition; part of the loop, the execution of the loop stops, when any of the conditional statement evaluates to be false.

PHP Online Test


« Previous Tutorial Next Tutorial »