While Loops

While Loop
While Loop

while Loops:
⦁ The operation is slightly different than the for loop , but the same operation can be implemented by for, while or do-while loops. the operation of the while loop is such that, first the condition is checked.if the condition is true , then the statement is executed the condition is again checked and this keeps on repeating until the condition is false.
SYNTAX of while loop

while(condition)
{
-
statements;
-
}
Example for while loop:
<?php
         $i = 0;
         $num = 50;
         
         while( $i < 10) {
            $num--;
            $i++;
         }
         
         echo ("Loop stopped at i = $i and num = $num" );
      ?>

OUTPUT:
Loop stopped at i = 10 and num = 40 
Example for While loop:

<?php  
2.$n=1;  
3.while($n<=10){  
4.echo "$n<br/>";  
5.$n++;  
6.}  
7.?>  

Output:

1
2
3
4
5
6
7
8
9
10
Translate »