while loop in PHP



while statement is using for making a loop in PHP. The while loop will execute as long as the expression with while statement is TRUE. The loop will terminate when the expression return FALSE.

Its structure will be

while ( expression )
{
// Operation statements
}

We can use nested while structures also like
while ( expression 1 )
{
while ( expression 2 )
{
// Operation Statements
}
}

See the sample PHP programs with while statements

<?php
$x=0;
while ( $x < 3)
 {
  echo $x." ";
 $x = $x +1;
 }
?>

It will output the result
0 1 2

See the sample PHP programs with nested while statements

<?php
$a = 0;
while ( $a < 3)
{
$a++;
$b = 0;
while ( $b < 3)
{
 $b++;
 echo $a*$b." ";
}
echo "";
}
?>

It will output the result
1 2 3
2 4 6
3 6 9