PHP Do While Statement

What is Do While Structure?

Explanation

Do While statement is same as the while statement, the only difference is that it evaluates the expression at the end.

Syntax


do
{
Statements
}while (expr1);

In do while, the statements are executed first, then the expr1 is checked, only drawback of Do While is that it may execute statements once even if the condition is false.

Example :


<?php
$c =1;
do {
echo $c;
}
while ($c>5);
?>

In the above example the value of $c should be displayed only if its greater than 5,but using the Do while statement it displays the value of $c as 1 first and then checks the expression or condition.

Example :


<?php
$c = 6;
do
{
if ($c>5)
{
echo "Enter the a value less than 5";
break;
}
}
while ($c<5);
?>

Use the "Break" statement to terminate the code execution in a loop

PHP Topics


Ask Questions

Ask Question