In the last tutorial, you learned how PHP Control Structures help your script make decisions. But what happens when you need to repeat the same action multiple times, like printing numbers from 1 to 100 or processing every item in a list? Writing that manually would be exhausting. This is exactly the problem PHP Loops solve.

This is Tutorial 5 of 10 in our PHP Basics for Beginners series. In this tutorial, you will learn all four major types of PHP Loops and understand exactly when to use each one.

What are PHP Loops?

PHP Loops are used to execute a block of code repeatedly, either a fixed number of times or until a certain condition becomes false. Instead of writing the same line of code over and over, PHP Loops let you define the logic once and let PHP handle the repetition automatically.

PHP provides four main types of loops, each suited to different situations: for, while, do-while, and foreach. Understanding when to use each type of PHP Loops will make your code cleaner and more efficient.

The for Loop

The for loop is one of the most commonly used PHP Loops when you already know exactly how many times you want to repeat something. It consists of three parts: initialization, condition, and increment, all written on a single line.

<?php
for ($i = 1; $i <= 5; $i++) {
    echo "Number: " . $i . "<br>";
}
?>

In this example, the loop starts at 1, continues as long as the value is less than or equal to 5, and increases by 1 after each round. This makes the for loop perfect for counting tasks and situations with a known repeat count.

The while Loop

Among PHP Loops, the while loop is used when you want to repeat code as long as a condition remains true, but you may not know exactly how many times it will run in advance.

<?php
$count = 1;

while ($count <= 5) {
    echo "Count: " . $count . "<br>";
    $count++;
}
?>

It is extremely important to update the variable inside a while loop, just like the increment shown above. Forgetting this step is a common mistake that creates an infinite loop, causing your script to run forever and crash the page.

The do-while Loop

The do-while loop is a slightly different variation among PHP Loops. Unlike a regular while loop, it always executes the code block at least once before checking the condition.

<?php
$number = 1;

do {
    echo "Number: " . $number . "<br>";
    $number++;
} while ($number <= 5);
?>

This behavior is useful when you need guaranteed execution at least one time, such as displaying a menu once before checking whether the user wants to continue or exit.

The foreach Loop

The foreach loop is a specialized type among PHP Loops, designed specifically for working with arrays. It automatically goes through every item in an array without requiring you to manage a counter variable manually.

<?php
$fruits = array("Apple", "Banana", "Mango");

foreach ($fruits as $fruit) {
    echo $fruit . "<br>";
}
?>

The foreach loop is by far the easiest and safest way to loop through arrays, and you will use it constantly once you start working more deeply with array data later in this series.

Using break Inside PHP Loops

Sometimes you need to stop a loop early before it naturally finishes. The break keyword immediately exits the loop, regardless of the original condition.

<?php
for ($i = 1; $i <= 10; $i++) {
    if ($i == 5) {
        break;
    }
    echo $i . "<br>";
}
?>

Using continue Inside PHP Loops

The continue keyword works differently from break. Instead of stopping the entire loop, it skips only the current iteration and moves on to the next one, keeping the loop alive.

<?php
for ($i = 1; $i <= 10; $i++) {
    if ($i % 2 == 0) {
        continue;
    }
    echo $i . "<br>";
}
?>

This example skips every even number and only prints odd numbers, demonstrating how break and continue give you fine control over how your PHP Loops behave during execution.

Choosing the Right Loop

Use a for loop when the number of repetitions is known in advance. Use a while loop when repetition depends on a condition that may change unpredictably. Use do-while when the code must run at least once regardless of the condition. Use foreach whenever you are working directly with arrays. For complete official documentation, refer to the official PHP loops manual.

What’s Next?

You now understand how PHP Loops work using for, while, do-while, and foreach, along with break and continue for extra control. In the next tutorial, we will explore PHP Functions, which allow you to organize your code into reusable blocks.

If you missed the previous lesson, check out Tutorial 4: PHP Control Structures, or explore all lessons in our PHP category.

Practice Exercise

Complete the following tasks to reinforce what you learned in this tutorial:

  • Create a PHP file named loops-practice.php
  • Write a for loop that prints numbers from 1 to 10
  • Write a while loop that prints all even numbers between 1 and 20
  • Write a do-while loop that prints “Welcome” exactly one time, even if the condition is false
  • Create an array of five of your favorite movies and use foreach to print each one
  • Inside a for loop from 1 to 10, use break to stop the loop when the number reaches 7
  • Inside a for loop from 1 to 10, use continue to skip and not print multiples of 3

Bonus Challenge: Write a nested loop (a for loop inside another for loop) that prints a multiplication table for numbers 1 through 5, formatted clearly using line breaks.

About Author

Arshad Sultan

Arshad Sultan is the Founder of Tutorials Ocean and a Technical Architect & Full-Stack Developer with 15+ years of experience in PHP, Laravel, CRM development, API integrations, and AI-powered automation. He also runs Rapid Code, a software and AI automation agency, and has trained hundreds of students across Karachi's IT institutes since 2010.

Leave a Reply

Your email address will not be published. Required fields are marked *