PHP - Statement While loop

Introduction

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

Demo

<?php
     $i = 1; //w  w w. j  a v a 2  s . co  m
     while ($i < 4) { 
         echo $i . " "; 
         $i++; 
     } 
?>

Result

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