PHP While Loop Tutorial

PHP While Loop Tutorial

  • PHP
  • 6 mins read

In this tutorial, you will learn about while loops in PHP.

The while loop is used to execute a block of codes until a certain condition is reached.
It is also called an entry controlled loop.

The general syntax of while loop is given below:

while(test condition)
{
body of the loop
}

For while loop, test condition will be evaluated and tested. If it returns 1 as true, then the body of the loop will be executed. After the execution of the loop, the test condition will be evaluated and tested again. If it is true the body of the loop will be executed again.

The process will be continued until the test condition becomes false. Then control is transferred out of the loop.

If the body of the loop contains one or more statements, braces are needed.

Example 1 of the while loop:

<html>
<body>
<?php
$a=0;
while($a<5)
{
echo "Gate 2019";
$i=$i+1;
}
?>
</body>
</html>

Explanation of the above example:

When $a=0; will be executed. Then 0 will be assigned to $a. Then test condition($a<5)will be evaluated and tested if it returns true as 1, then the body of the loop, here loop body is containing two statements, firstly first statement(i.e., echo “Gate 2019”)will be executed. Then the second statement $i=$i+1; will be executed. After that test condition($a<5) will be evaluated and tested again. If it returns true as 1, then the body of the loop will be executed again. This process will be continued until $a<5 becomes false. Here “Gate 2019” will be printed 5 times.

Example 2 of the while loop:

<html>
<body>
<?php
$a=153;
$b=$a;
$sum=0;
while($b!=0)
{
$temp=$b%10;
$sum=$sum+$temp*$temp*$temp;
$b=(int)($b/10);
}
if($sum==$a)
echo "The number is an Armstrong number";
else
echo "The number is not an armstrong number";
?>
</body>
</html>

Explanation of the above example:

This is a program for checking where a number is an Armstrong or not. Armstrong number is a number where the sum of cubes of each digit is equal to
that number. Here 153=1*1*1+5*5*5+3*3*3.

Here when $a=153; is executed. Then 153 is assigned to $a. When $b=$a; will be executed when the value of $a(i.e 153)will be assigned to $b.
After the execution of $sum=0;, 0 will be assigned to $sum. When while loop will be started to execute, then $b!=0 will be evaluated and tested. If it returns 1,
then the body of the loop, where the loop body is containing three statements.

Firstly %temp=$b%10; will be executed.Then(153%10=3)3 will be assigned to $temp.
after that $sum=$sum+$temp*$temp*$temp; will be executed.So that 0+3*3*3=27 will be assigned to $sum. Then $b=(int)($b/10); will be executed. Here (int)($b/10)
will return 15 and 15 will be assigned to $b. Then test condition(i.e $b!=0)will be evaluated and tested. If it returns true as 1,then $temp=$b%10;will be executed.
So($b%10)=5 will be assigned to $temp. Then $sum=$sum+$temp*$temp*$temp; will be executed and 27+5*5*5=152 will be assigned to $sum. Then $b=(int)($b/10)
will be executed.Then (int)($b/10)=1 will be assigned to $ $b.

Then test condition(i.e $b!=0)will be executed and tested again. Then it returns 1,So $temp=$b%10;
will be executed again. So $b%10=1 will be assigned to $temp. Then $sum=$sum+$temp*$temp*$temp; will be executed again. So 152+1*1*1=153 will be assigned to $sum.

Then $b=(int)($b/10);will be executed again and (int)($b/10) will return 0. After that $b!=0 will be evaluated and tested again.Here it will not return 1.
So the loop will be terminated. Then $sum==$a will be evaluated and tested, and here it will return 1. So echo “The number is an Armstrong number”; will be executed. So the output will be “The number is an Armstrong”.

NB:(int)($b/10) is used to convert a float value to an integer.

Example 3 of the while loop:

<html>
<body>
<?php

$a=100;
while($a<0)
{
echo "hello";
$a=$a-1;
}
?>
</body>
</html>

output: Here hello will be displayed for 100 times\

Example 4 of the while loop:

<html>
<body>
<?php
$a=1;

while($a<5)
{
$b=1;
while($b<=$a)
{
echo "*";
$b=$b+1;
}
echo "<br>";
$a=$a+1; 
}
?>

</body>
</html>

Output:
*
**
***
****

Explanation:

When $a=1; will be executed, then 1 will be assigned to $a. Then the outer loop will be started to execute. Firstly $b<=$a will be evaluated and tested. If it returns true as 1, then the body of the outer loop, here $b=1; will be executed. Then the inner loop will be started to execute. Here firstly $b<=$a will be evaluated and tested. If it returns 1 as true then the body of the inner loop, here echo “*”; will be executed(here * will be printed for one time).

Then $b=$b+1; will be executed.Here firstly the inner loop will be run for one time. Then echo “
”; will be executed and a new line will be created.

Then $a=$a+1; will be executed.Then the test condition of the outer loop will be evaluated and tested. If it returns 1 then the body of the outer loop, here $b=1; will be executed again. Then the inner loop will be started to execute again. Here $b<=$a will be evaluated and tested again. If it returns 1 as true then the body of the inner loop, here echo “*”; will be executed again and one * will be printed.

Then $b=$b+1; will be executed. Then the test condition $b<=$a will be evaluated and tested again. If it returns 1 the echo “*”; will be executed again and one * will be printed again. Then $b=$b+1 will be executed again After that the test condition $b<=$a will be evaluated and tested again, and here it will not return 1 So the inner loop will be terminated. Actually here inner loop will run for two times. Then echo “
”; will be executed and a new line will be created again. Then $a=$a+1; will be executed and then the test condition of the outer loop(i.e., $a<5) will be evaluated and tested again. This process will be continued until $a<5 becomes false.