The Do While Loop

The do while loop

The do while loop is similar in syntax and purpose to the while loop. The do/while loop construct moves the test that continues the loop to the end of the code block. The code is executed at least once, and then the condition is tested. For example:

$c = 6;
do {
  echo 'Hi';
} while ($c < 5);

Even though $c is greater than 5, the script will echo "Hi" to the page one time.

PHP's do/while loop is not commonly used.


This example will output the factorial of $number (product of all the numbers from 1 to it).

Crystal Clear app terminal.png PHP Code:

$number = 5;
$factorial = 1;
do {
  $factorial *= $number;
  $number = $number - 1;
} while ($number > 0);
echo $factorial;

Crystal Clear app kscreensaver.png PHP Output:


Crystal 128 yast default.png HTML Render:


The continue statement

The continue statement causes the current iteration of the loop to end, and continues execution where the condition is checked - if this condition is true, it starts the loop again. For example, in the loop statement:

$number = 6;
for ($i = 3; $i >= -3; $i--) {
  if ($i == 0) {
  echo $number . " / " . $i . " = " . ($number/$i) . "<br />";

the statement is not executed when the condition i = 0 is true.

For More Information

  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.



Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.

Manage research, learning and skills at Create an account using LinkedIn to manage and organize your omni-channel knowledge. is like a shopping cart for information -- helping you to save, discuss and share.

  Contact Us