PHP do while Loop

Table of Contents

PHP do while Loop

The do...while loop will always execute the block of code at least once, it will then check the condition, and repeat the loop while the specified condition is true.

<h4>do while Loop</h4> <p>The do...while loop - Loops through a block of code once, and then repeats the loop as long as the specified condition is true.</p> <p>The do...while loop will always execute the block of code at least once, it will then check the condition, and repeat the loop while the specified condition is true.</p> <?php $i = 1; do { echo $i; $i++; } while ($i < 6); ?> <hr> <p>Set $i = 8, then print $i as long as $i is less than 6:</p> <?php $i = 8; do { echo $i; $i++; } while ($i < 6); ?>

Example 1

Result View Example

PHP break Statement

With the break statement we can stop the loop even if the condition is still true:

<h4>break Statement</h4> <p>Stop the loop when $i is 3:</p> <?php $i = 1; do { if ($i == 3) break; echo $i; $i++; } while ($i < 6); ?>

Example 2

Result View Example

PHP continue Statement

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

<h4>continue Statement</h4> <p>Stop, and jump to the next iteration if $i is 3:</p> <?php $i = 0; do { $i++; if ($i == 3) continue; echo $i; } while ($i < 6); ?>

Example 3

Result View Example

Document

Document in project

You can Download PDF file.

Reference