How to use do-while loop in PHP
In this article, I will explain how the do-while loop can be used in PHP.
do-while Loop in PHP
- The do-while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.
- The do-while would execute its statements at least once, even if the condition fails for the first time itself.
Syntax
do
{
code to be executed;
}
while (condition);
|
Example
The following example show the do-while loop that starts with a=1. It will then increment a with 1, and write some output. Then the condition is checked, and the loop will continue to run as long as a is less than, or equal to 5:
<html>
<body>
<h2 style="color: darkkhaki;">do-While Loop example</h2>
<?php
$a=1;
do
{
$a++;
echo "The number is " . $a . "<br />";
}
while ($a<=5);
?>
</body>
</html>
|
Output
You may also want to read these related articles here
Ask Your Question
Got a programming related question? You may want to post your question here