🔁 PHP Control Flow & Logic
Estimated reading: 3 minutes 263 views

PHP While Loop – Syntax, Examples, and Best Practices

A practical guide to using the while loop in PHP for condition-based iteration.


Introduction – Why Use the while Loop?

The while loop in PHP is ideal when you need to execute code repeatedly as long as a specific condition is true. It is widely used when the number of iterations is unknown beforehand — like reading from a database, waiting for a flag, or processing user input.

In this guide, you’ll learn:

  • Basic syntax and usage of while loops
  • Real-world examples and variations
  • How to use break and continue
  • Best practices for avoiding infinite loops

Basic while Loop Syntax

$i = 1;

while ($i <= 3) {
    echo "Iteration: $i <br>";
    $i++;
}

Output:

Iteration: 1
Iteration: 2
Iteration: 3

The loop runs while $i <= 3. It stops when the condition becomes false.


Example – Print Even Numbers

$num = 2;

while ($num <= 10) {
    echo "$num ";
    $num += 2;
}

Output: 2 4 6 8 10

Useful for generating sequences, such as pagination or math tables.


⛓️ Controlling Loop Execution

Exit Early with break

$i = 1;

while ($i <= 5) {
    if ($i == 3) break;
    echo "$i ";
    $i++;
}

Output: 1 2

Stops the loop when $i equals 3.


Skip Iteration with continue

$i = 0;

while ($i < 5) {
    $i++;
    if ($i == 3) continue;
    echo "$i ";
}

Output: 1 2 4 5

Skips printing the value 3.


Example – Wait for a Flag

$flag = false;
$attempts = 0;

while (!$flag && $attempts < 3) {
    echo "Trying...<br>";
    $attempts++;

    if ($attempts == 2) {
        $flag = true;
    }
}

Output:

Trying...
Trying...

Use in polling, retry logic, or waiting for responses.


Avoiding Infinite Loops

Bad example:

$i = 1;
while ($i <= 3) {
    echo $i;
    // missing $i++
}

Result: Infinite loop!

Always update your control variable inside the loop unless using a break.


When to Use while

SituationUse while?
Number of loops is unknown
Reading file line-by-line
Condition must be re-checked often
Fixed iteration count Prefer for

Best Practices

  • Always modify the condition variable inside the loop
  • Use break for emergency exits
  • Use continue to skip logic
  • Avoid writing while(true) unless absolutely necessary
  • Keep logic inside the loop clean and efficient

Summary – Recap & Next Steps

The while loop helps you execute logic based on runtime conditions — giving you flexibility and control when you don’t know how many iterations will be needed.

Key Takeaways:

  • Runs as long as the condition is true
  • Perfect for unknown iteration counts
  • Use break and continue to control execution
  • Avoid infinite loops by managing loop variables

Real-World Use Cases:
Processing file uploads, polling for status, retrying failed requests, database cursor reads.


Frequently Asked Questions (FAQs)

How is while different from for?
while is better when you don’t know how many times the loop should run.

Can I use break inside a while loop?
Yes, to exit early based on a condition.

Can I use continue in while?
Yes, to skip the rest of the loop body and move to the next condition check.

What causes infinite loops in while?
Forgetting to update the condition variable or using a condition that never becomes false.


Share Now :
Share

🔁 PHP — While Loop

Or Copy Link

CONTENTS
Scroll to Top