Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

PHP Programming/The do while Loop

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

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: <?php $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. Example This example will output the factorial of $number (product of all the numbers from 1 to it). $number = 5; $factorial = 1; do { $factorial *= $number; $number = $number - 1; } while ($number > 0); echo $factorial; 120 120 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: <?php $number = 6; for ($i = 3; $i >= -3; $i--) { if ($i == 0) { continue; } echo $number . " / " . $i . " = " . ($number/$i) . "<br />"; } ?> the statement is not executed when the condition i = 0 is true. For More Information PHP Manual: Control Structures: do-while