Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. PHPBeginner - About while and do...while Loops

About while and do...while Loops - Images: Japanese

Hey everyone, hope you're doing well!

This time we're covering the while statement and the do...while statement.

Let's start with the while statement. Like the for statement, the while statement is a syntax for running loop processes. It can actually build loops more concisely than a for statement. The flip side is that one small mistake in your code can easily create an infinite loop, so please be extra careful about that.

In terms of how often you'll use it, the for statement and the foreach statement (covered in the next article) are far more common — you won't reach for the while statement all that often.

One key difference from the for statement is that the while statement is suited for situations where the number of loop iterations is not predetermined. With the for statement, you specify the number of iterations directly in the syntax itself. The while statement has no such built-in mechanism for specifying a count.

Let's take a quick look back at the for statement from the previous article.

<?php
for($num = 0; $num < 10; ++$num){
    echo $num;
}

If you look at the condition part inside the (), you'll see $num < 10 — meaning the loop runs exactly 10 times and then stops. The while statement, on the other hand, has no syntax for defining that count, so it ends up looking like this.

<?php
$num = 0;

while($num < 10){
    var_dump($num);
    ++$num;
}

Compared to the for statement, it's much more concise. Let's walk through what's happening step by step.

First, we declare the variable $num and assign it an initial value of 0. Then after while, there's a (). That () holds the condition expression — same as in a for statement. As long as the condition inside () holds (i.e., evaluates to true), the code inside {} keeps running in a loop. As you can see, there's no initial value, condition, or update specification like in the for statement — it's very simple.

If ++$num; didn't exist in the code above, $num would stay at 0 forever, and $num < 10 would be true forever. That's how easy it is to accidentally create an infinite loop, so please be careful.

<?php
$num = 0;

while($num < 10){ // This creates an infinite loop.
    var_dump($num);
}

Please don't run the code above. Keep in mind that when writing a while statement, you must always include logic inside the loop that changes the value of the variable used in the condition.

In PHP, you can also write : and endwhile; instead of {} for a while statement, like this.

<?php
$num = 0;

while($num < 10):
    var_dump($num);
    ++$num;
endwhile;

It's not a widely used syntax, but it's worth knowing about.

Also, the break statement used to exit a switch statement also works inside a while statement. Here's how you'd use it.

<?php
$num = 0;

while(true){
    var_dump($num);
    if($num >= 5) break; // Exits the loop when '$num' reaches 5 or more.
    ++$num;
}

In this case, the () of the while statement contains true directly, so it looks like an infinite loop at first glance — but there's an if($num >= 5) break; inside. When $num reaches 5 or more, break is executed, so the loop only runs for values 0 through 5.

There's also something called the continue statement that we haven't covered yet, so let's take a look. Like the break statement, the continue statement lets you exit the current iteration — but unlike break, it doesn't end the loop entirely. Instead, it stops executing the remaining statements in the current iteration and jumps back to the top of the loop. Here's an example.

<?php
$num = 0;

while($num < 10){
    ++$num;
    var_dump($num);
    if($num > 5) continue; // When '$num' is 6 or more, the statements below are not executed.
    echo $num . ' is 5 or less.<br>';
}

Running this produces the following output.

int(1) 1 is 5 or less.
int(2) 2 is 5 or less.
int(3) 3 is 5 or less.
int(4) 4 is 5 or less.
int(5) 5 is 5 or less.
int(6) int(7) int(8) int(9) int(10)

In this case, 'echo $num . ' is 5 or less.<br>;' only executes when '$num' is 5 or less. When it's higher, the loop jumps back to the top.

That said, you can achieve the same result using an if statement instead of 'continue'. And since an if statement lets you wrap logic in '{}' as a block, it's honestly easier to work with in most cases.

<?php
$num = 0;

while($num < 10){
    ++$num;
    var_dump($num);
    if($num > 5) continue; // With 'continue', every statement below this line is unconditionally skipped.
    echo $num . ' is 5 or less.<br>';
}
<?php
$num = 0;

while($num < 10){
    ++$num;
    var_dump($num);
    if($num <= 5){ // With an if statement, you can use '{}' to block off your logic — easier to manage.
        echo $num . ' is 5 or less.<br>';
    }
    // Any code written after this point will run normally.
}

So the continue statement isn't used all that often. If you forget it, just look it up — that's totally fine.

Now, there's a variant of the while statement called the do...while statement. This version always executes the loop body at least once. The syntax looks like this.

<?php
$num = 0;

do{
    var_dump($num);
    ++$num;
}while(false);

The syntax might look a bit confusing, so let's break it down carefully.

The code inside the 'do' block '{}' is the loop body, and it always runs at least once on the first pass. After that, there's a 'while()' at the end. The condition inside that 'while()' determines whether the loop continues. If the condition is 'true', the loop continues; if it's 'false', the loop exits.

So in the example above, 'var_dump($num);' and '++$num' run first, and then the condition in 'while()' is evaluated. Since it's 'false', the loop exits after that first run.

Let's compare it with a regular while statement. The following code never executes at all because the condition inside () is false right from the start.

<?php
$num = 0;

while(false){
    var_dump($num);
    ++$num;
}

If you want to guarantee that the loop runs at least once even with that condition, you can rewrite it using 'do...while' like this.

<?php
$num = 0;

do{
    var_dump($num);
    ++$num;
}while(false);

And that wraps up our look at the while statement and do...while statement. The syntax is much simpler than the for statement, so it shouldn't be too tricky to pick up. Just make sure you don't accidentally build an infinite loop — that's the one thing to watch out for!

In the next article, we'll be covering the foreach statement. See you there!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .