codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

12 / 56 topics
7Control Structures8If, Else, and Elseif Statements9Switch Case Statement10For Loop11While Loop12Do While Loop13Foreach Loop
Tutorials/PHP/Do While Loop
🐘PHP

Do While Loop

Updated 2026-05-15
10 min read

Do While Loop

Introduction

In PHP, loops are essential for executing a block of code repeatedly under certain conditions. One type of loop that stands out is the do while loop. Unlike other looping constructs like for or while, the do while loop executes its body at least once before checking the condition. This makes it particularly useful when you need to ensure that a piece of code runs at least one time, regardless of whether the condition is initially true or false.

Concept

The do while loop in PHP follows this basic structure:

do {
    // Code to be executed
} while (condition);

Here's how it works:

  1. The body of the loop is executed first.
  2. After executing the body, the condition is evaluated.
  3. If the condition evaluates to true, the loop repeats from step 1.
  4. If the condition evaluates to false, the loop terminates.

The key characteristic of a do while loop is that it guarantees at least one execution of its body before checking the condition. This can be particularly useful in scenarios where you need to prompt the user for input or perform an action that must occur at least once.

Examples

Example 1: Basic Usage

Let's start with a simple example where we print numbers from 1 to 5 using a do while loop.

<?php
$number = 1;

do {
    echo $number . "\n";
    $number++;
} while ($number <= 5);
?>

Output:

Output
1
2
3
4
5

In this example, the loop prints numbers from 1 to 5. The variable $number is initialized to 1, and it is incremented in each iteration. The loop continues as long as $number is less than or equal to 5.

Example 2: User Input

A common use case for a do while loop is when you need to ensure that the user inputs valid data. For instance, let's create a program that repeatedly asks the user to enter a number between 1 and 10 until they provide a valid input.

<?php
$number = 0;

do {
    echo "Enter a number between 1 and 10: ";
    $number = trim(fgets(STDIN));
} while ($number < 1 || $number > 10);

echo "You entered: " . $number;
?>

Output (Example):

Output
Enter a number between 1 and 10: 15
Enter a number between 1 and 10: -3
Enter a number between 1 and 10: 7
You entered: 7

In this example, the loop continues to prompt the user for input until they enter a number within the specified range (1 to 10). The fgets(STDIN) function is used to read input from the terminal.

Example 3: Infinite Loop with Break

While the do while loop guarantees at least one execution, it can also be used in an infinite loop scenario where you use a break statement to exit based on certain conditions. Here's an example:

<?php
$counter = 0;

do {
    echo "Counter: " . $counter . "\n";
    $counter++;
    if ($counter == 5) {
        break;
    }
} while (true);
?>

Output:

Output
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4

In this example, the loop runs indefinitely because the condition is always true. However, the loop exits when $counter reaches 5 due to the break statement.

What's Next?

After mastering the do while loop, you might want to explore other types of loops in PHP. The next topic in our curriculum will cover the foreach loop, which is particularly useful for iterating over arrays and objects. Stay tuned!

If you have any questions or need further clarification on the do while loop, feel free to ask in the comments below!


PreviousWhile LoopNext Foreach Loop

Recommended Gear

While LoopForeach Loop