PHP - Statement While loop


The while loop executes a block of code until the expression to evaluate returns false.


     $i = 1; /*from  ww  w  .ja va 2 s  .  c om*/
     while ($i < 4) { 
         echo $i . " "; 


Here, we define a variable with value 1.

Then we have a while clause in which the expression to evaluate is $i < 4.

This loop executes the content of the block of code until that expression is false.

Inside the loop we are incrementing the value of $i by 1 each time, so the loop ends after 4 iterations.

The last value printed is 3, so at that time the value of $i was 3.

After that, we increased its value to 4, so when the while clause evaluates if $i < 4, the result is false.

Related Topics