PHP For Loops

Instead of executing a task again and again, using loop is an efficient way of executing task in programming / Scripting world.

FOR LOOP:

For loop, as name says, looping a task until unless a condition is met.

Syntax:

for(initial value; condition; increment/decrement){

//execute required task 

}

To better understand the syntax, lets take one example.

Suppose I want to print 1 to 10 on the webpage. So the code is as below.

<?php

echo 1;

echo 2;

echo 3;

echo 4;

echo 5;

echo 6;

echo 7;

echo 8;

echo 9;

echo 10;

?>

The above code is going to print 1 to 10.

What if , I am required to print 1 to 1000. It would be time consuming process. So for such scenario, we use for loop. Using for loop we can execute any number of task.

Lets print 1 to 100 using for loop.

Before using for loop, we have to ready with following three factors:

  1. Starting value (here starting value is 1)
  2. condition (Up to what value we want to print value. Here we want to print upto 100. The condition would be: less than 101 or less than equal to 100)
  3. Upon completion of a loop, what it should do? (In our example, upon completion of each loop, we want to increment the value by 1)

In our example, starting value is 1. It should loop until the value 100 is printed. Therefore the condition is the loop should keep looping until 100 is printed. At the end of the each loop, it should increase the value by 1.

The code would be as below:

<?php

for($i=1; $i<=100; $i++){

echo $i;

}

?>

The above code would print 1 to 100. See, for loop saved our time and effort. To print 1 to 100, we could have typed 100 echo statements. But just three lines did the job. This is the power of for loop.

  • Observe in the for loop, the three factors are passed inside the bracket ($i=1; $i<=100; $i++).
  • Observe, each factor ends with semi colon except the increment operation.
  • Remember, the first factor would be always the initialisation value. That is from which value the loop should start. It should end with semicolon. (Here the initial value is 1. So we have declared a variable $i=1)
  • Then, the second factor, condition statement is placed. Here the condition is $i less than equal to 100. Because our loop should keep looping until unless the value reaches 100. This condition ends with semi colon.
  • Then the third factor, what should it do, upon completion of a loop. It should increment the value of $i by 1. Thats why we have used increment operator. Note here, the third factor does not end with semi colon.
  • Then inside the curly braces, we put the required task to execute in each loop. Here task is to print the value and that is $i.

So what is happening in this for loop block?

  • This for loop loops for 100 times.
  • In first loop, the value is initialised to 1. That is $i=1; Then it checks the condition. Here the condition is $i<=100. Here the condition met. Because the value of $i in first loop is 1. Then it prints 1. Because the task is to print 1. Then it looks for the third factor in the for loop. The third factor instructs for loop what to do upon completion of the loop. Here it instructs to increment the value of $i by 1. ($i++).
  • Now, it comes to second loop. In first loop, towards the end, the value of $i is incremented by 1. Now in second loop, the value of $i is, 2. Then it checks for the condition. The condition here is $i<=100. In second loop too, the condition is met. (Because value of $i in second loop is 2 which is less than 100). Then it prints the value of $i. After printing the value of $i, it looks for the third factor, and the third factor tells to increment the value of $i by 1. Towards the end of second loop, the value of $i has become 3.
  • Now, it comes to third loop. In second loop, towards the end, the value of $i incremented by 1 and now in third loop, the value of $i is 3. Then it checks for the condition. The condition is $<=100. So here the condition met. It prints the value of $i and that is 3. Then it looks for the third factor. The third factor says to increment the value of $i by 1. So at the end of third loop the value of $i becomes 3.
  • (And so on)…. In 99th loop, the value of $i is 99. Then it checks for the condition. The condition is $i<=100. In 99th loop, the condition too met. Then it will print $i that is 99. Then it will look for third factor. The third factor says to increment its value by 1. Now at the end of 99th loop, the value of $i becomes 100.
  • And now, it comes to 100th loop. In this loop the value of $i is 100. Then it checks for the condition. The condition is $i<=100. Condition too met here. Then it will print $i that is 100. Then it looks for third factor. It says to increment the value of $i by 1. Now towards the end of 100th loop the value of $i becomes 101.
  • And now, it comes to 101th loop. Here the value of $i is 101. It checks the condition. Here condition does not meet. Because the condition is $i<=100. But the value of $i is 101. So here the loop breaks. It wont print the value of $i and comes out of the loop. 

This way, for loop works.

Warning: if you forget to mention the condition, then the loop lands in infinite loop. Because for loop only stops when a condition did not meet. If we don’t mention the condition then, it would go into infinite loop. So be careful while using for loop. 

Assignment:

  • Print 1 to 100 using for loop
  • Print 1000 to 1 in reverse order (Hint: for($i=1000;$i>=1;$i++))