PHP While and Do-while Loop

In PHP, loops are used to execute a block of code repeatedly based on a condition. The while and do-while loops are two fundamental looping constructs that allow for repetitive execution. Here’s a detailed look at both:

while Loop

The while loop repeatedly executes a block of code as long as a specified condition evaluates to true. The condition is checked before each iteration.

Syntax:

php
while (condition) {
    // Code to execute while the condition is true
}


Example:

php
$counter = 1;
while ($counter <= 5) {
    echo "Counter: $counter
";
    $counter++;
}


In this example:

    The loop starts with $counter set to 1.
    It prints the value of $counter and then increments it.
    The loop continues as long as $counter is less than or equal to 5.
    Output:

    makefile

    Counter: 1
    Counter: 2
    Counter: 3
    Counter: 4
    Counter: 5

do-while Loop

The do-while loop executes a block of code once before checking the condition. If the condition is true, the loop continues to execute the code block. This guarantees that the code block is executed at least once.

Syntax:

php
do {
    // Code to execute at least once and then while the condition is true
} while (condition);


Example:

php
$counter = 1;
do {
    echo "Counter: $counter
";
    $counter++;
} while ($counter <= 5);


In this example:

    The loop starts by executing the code block, printing $counter and then incrementing it.
    It then checks if $counter is less than or equal to 5.
    The loop continues as long as the condition is true.
    Output:

    makefile

    Counter: 1
    Counter: 2
    Counter: 3
    Counter: 4
    Counter: 5

Key Differences

Condition Check Timing:
        while: Checks the condition before each iteration. If the condition is initially false, the code block may not execute at all.
        do-while: Checks the condition after each iteration. The code block will always execute at least once.

Use Case:
        while: Use when you want to continue looping as long as a condition is true and may need zero or more iterations.
        do-while: Use when you need to ensure that the code block executes at least once before the condition is tested.

Example of while and do-while Loops with Early Exit

Both while and do-while loops can use the break statement to exit the loop early and the continue statement to skip the current iteration and proceed to the next one.

Using break and continue in while Loop:

php
$counter = 1;
while ($counter <= 10) {
    if ($counter == 6) {
        break; // Exit the loop if counter is 6
    }
    if ($counter % 2 == 0) {
        $counter++;
        continue; // Skip printing if counter is even
    }
    echo "Counter: $counter
";
    $counter++;
}


Using break and continue in do-while Loop:

php
$counter = 1;
do {
    if ($counter == 6) {
        break; // Exit the loop if counter is 6
    }
    if ($counter % 2 == 0) {
        $counter++;
        continue; // Skip printing if counter is even
    }
    echo "Counter: $counter
";
    $counter++;
} while ($counter <= 10);


Conclusion

while Loop: Checks the condition before executing the code block. Useful when the number of iterations is not known beforehand and the loop might not execute at all if the condition is initially false.
    do-while Loop: Executes the code block once before checking the condition. Useful when you need the code block to run at least once regardless of the condition.

Understanding and using these loops effectively will help you handle repetitive tasks and control the flow of your PHP scripts efficiently.




  • To Share this Link, Choose your plateform


Our Other Tutorials