PHP While loops

While loop in PHP, is another efficient way to execute task in loop. While loop can also be considered to alternative to for loop.

While Loop:

While loop is used to execute task in loop.

While loop syntax:

initial value

while (condition){

// execute required task

// what should it do at the end of a loop Increment / Decrement

}

If you are not aware why loops are used then read: PHP for loops

Example:

Lets print 1 to 100 using while loop.

Before using while loop, we need to understand three required factors for the while loop.

  • First factor is, we need to know the starting value. Here the starting value is 1.
  • Second factor is, we need to know the condition and that is upto what value it should keep on looping. (In this case, it should loop until the value 10 is printed. Therefore the condition is less than equal to 10)
  • Third factor is, what it should do, at the end of each loop. In this case, it should increment the value by 1 at the end of each loop.

Once we know above three factors, then we are ready to use while loop.

Remember, the task here is to print the value in each loop.

The code is:

<?php

$i=1;

while($i<=10){

echo $i;

$i++;

}

?>

The above code prints 1 to 10.

  • In the above code, first we have initialised the value. That is, $i=1.
  • Then inside while condition, we defined the condition. the condition is $i<=10
  • Then inside while loop block, we have defined the task. The task here is to print the value of $i. Immediate after the task, we have defined it should increment the value of $i by 1.

What is happening in the while loops?

  • The while loop, executes the task (task: print value of $i) in loops. Ten loops take place.
  • In the first loop, first it checks for the condition. Here the condition is, $i<=10. The value of $i is 1. Here the condition met. Then it prints the value of $i. That is, it prints 1. Then it increments the value of $i by 1. At the end of first loop, the value of $i becomes 2.
  • Then it comes into second loop. First it checks the condition. The condition is $i<=10. In second loop the value of $i is 2. (Because towards the end of second loop, the value of $i is incremented by 1). In second loop, the condition met. (That is, $i<=10). Then it prints the value of 2. Then it increments the value of $i by 1. Now the value of $i becomes 3.
  • Then it comes into third loop. First it checks the condition. The condition is $i<=10. In third loop, the value of $i is 3 (Because towards the end of second loop, the value of $i is incremented by 1). In third loop too, the condition is met. Then it prints the value of $i. That is 3. Then it increments the value of $i by 1. Now, the value of $i becomes 4.
  • And so on… in the ninth loop, $i value is 9. In ninth loop, the condition too is met. Because the condition is $i<=10. Then it prints the value of $i. That is 9. Then it increments the value of $i by 1. Now the value of $i becomes 10.
  • In the tenth loop, the value of $i is 10. It checks for the condition. Condition is $i<=10. In tenth loop, the condition too met. It prints the value of $i. That is 1o. Then it increments the value of $i by 1. Now the value of $i becomes 11.
  • Then it comes into 11th loop. In this loop, the value of $i is 11. It checks for the condition. Condition is $i<=10. But in this loop, the condition did not meet. Because the value of $i is 11. It breaks the loop and comes out of the loop.

This way, while loop works.

Warning: 

While using ‘while loop’, you have to be very careful. If you forget to mention the initial value, condition and increment or decrement operation then it would land into infinite loop. 

Assignment:

  • Print 1 to 1000 using while loop
  • Print 1000 to 1 using while loop (hints: $i=1000, while ($i>=1), $i++)