A While loop is used when we have to repeat a set of statements as long as the condition is false. It could be the best where number of repetition is not known earlier or in advance.

It can be explain in plain English as “Keep doing something until a condition is true”.

Syntax:

while (condition)

{

//  Set of statements goes here;

}

Example

<?php

$ct=5;

while($ct<=50)

{

echo  $ct . “<br>”;

$ct = $ct+5;

}

?>

Output:

5 10 15 20 25 30 35 40 45 50

The do…while loop example:

<?php

$i = 1;

do

{

$i++;

echo  $i . “<br>”;

}

while($i <= 3);

?>

 Output: 2 3 4

Difference Between while and do…while Loop: With a while loop, the condition to be evaluated is tested at the beginning of each loop statement, so if the conditional expression evaluates to false, the loop will never be executed.

With a do-while loop, on the other hand, the loop will always be executed once, even if the conditional expression is false, because the condition is evaluated at the end of the loop statement.