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

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

While Loop

Updated 2026-05-15
10 min read

While Loop

Introduction

In PHP, the while loop is a control flow statement that allows you to execute a block of code repeatedly as long as a specified condition remains true. This type of loop is particularly useful when the number of iterations is not known beforehand and depends on runtime conditions.

Concept

The basic syntax of a while loop in PHP is as follows:

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

Here's how it works:
1. The condition is evaluated.
2. If the condition is true, the code block inside the loop is executed.
3. Steps 1 and 2 are repeated until the condition becomes false.

The key point to remember is that the condition is checked before each iteration of the loop. This means that if the condition is initially false, the loop body will not be executed at all.

## Examples

### Example 1: Basic While Loop

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

```php
<?php
$counter = 1;

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

Output:

Output
1
2
3
4
5

In this example:

  • We initialize a variable $counter to 1.
  • The while loop checks if $counter is less than or equal to 5.
  • If the condition is true, it prints the current value of $counter and increments it by 1.
  • This process repeats until $counter becomes 6, at which point the condition fails, and the loop terminates.

Example 2: Infinite Loop

Be cautious with while loops as they can easily become infinite if the condition never becomes false. Here's an example of an infinite loop:

<?php
$counter = 1;

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

In this case, since $counter is never incremented inside the loop, the condition will always be true, resulting in an infinite loop. To avoid this, ensure that the loop modifies its condition variables appropriately.

Example 3: Using While Loop with Arrays

You can also use a while loop to iterate over arrays. Here's an example:

<?php
$fruits = ['apple', 'banana', 'cherry'];
$index = 0;

while ($index < count($fruits)) {
    echo $fruits[$index] . "\n";
    $index++;
}
?>

Output:

Output
apple
banana
cherry

In this example:

  • We have an array $fruits containing three elements.
  • We initialize $index to 0 and use a while loop to iterate over the array.
  • Inside the loop, we print the current fruit and increment the index.
  • The loop continues until $index is no longer less than the length of the array.

What's Next?

After mastering the while loop, you should explore the do...while loop, which is similar to a while loop but guarantees that the loop body is executed at least once. This can be particularly useful in scenarios where the condition might fail on the first check.

Stay tuned for more tutorials on PHP control flow and advanced programming concepts!


PreviousFor LoopNext Do While Loop

Recommended Gear

For LoopDo While Loop