PHP - Loop Types Explained with Examples

PHP loops example showing for while do while and foreach loop usage

What if you are asked in a practical exam to print numbers from 0 to 100 using a PHP script? Would you write echo 100 times, or look for a smarter and shorter solution? Writing 100 lines is clearly not efficient. This is where PHP Loops come into play. By using loops, you can write a single block of code that prints numbers from 0 to 100 in seconds, saving both time and effort.

In PHP programming, loops are used to execute a block of code multiple times without repeating it manually. This makes your code shorter, cleaner, and much more efficient.

If you are learning PHP or preparing for exams, understanding PHP loops is essential. In this complete guide, you will learn all types of loops in PHP with simple examples and real-world use cases.


What are PHP Loops?

PHP loops allow you to execute the same block of code multiple times based on a condition. Instead of writing repetitive code, loops help automate repeated tasks.

For example, printing numbers from 1 to 10 or processing multiple records from a database.


Types of Loops in PHP

  • for loop – used when number of iterations is known
  • while loop – runs as long as condition is true
  • do...while loop – runs at least once before checking condition
  • foreach loop – used for arrays

Loop Control Keywords:

  • break – stops the loop
  • continue – skips current iteration

1. PHP for Loop

The for loop is used when you already know how many times you want to execute a block of code.

Syntax

for (initialization; condition; increment) {
   // code to be executed
}

Example

<?php
$a = 0;
$b = 0;

for($i = 0; $i < 5; $i++) {
   $a += 10;
   $b += 5;
}

echo "At the end of the loop a = $a and b = $b";
?>

Output: At the end of the loop a = 50 and b = 25

This loop runs exactly 5 times, making it perfect for counting-based tasks.


2. PHP while Loop

The while loop executes a block of code as long as the condition is true.

Syntax

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

Example

<?php
$i = 0;
$num = 50;

while($i < 10) {
   $num--;
   $i++;
}

echo "Loop stopped at i = $i and num = $num";
?>

Output: Loop stopped at i = 10 and num = 40

This loop is useful when you don’t know how many times the loop will run in advance.


3. PHP do...while Loop

The do...while loop executes the code at least once, even if the condition is false.

Syntax

do {
   // code to be executed
} while (condition);

Example

<?php
$i = 0;

do {
   $i++;
} while($i < 10);

echo "Loop stopped at i = $i";
?>

Output: Loop stopped at i = 10

This is useful in scenarios where execution must happen at least once, like menu systems.


4. PHP foreach Loop

The foreach loop is used to iterate over arrays. It automatically moves through each element.

Syntax

foreach (array as value) {
   // code to be executed
}

Example

<?php
$array = array(1, 2, 3, 4, 5);

foreach($array as $value) {
   echo "Value is $value <br>";
}
?>

Output:

Value is 1
Value is 2
Value is 3
Value is 4
Value is 5

This loop is very useful for handling arrays and data collections.


Real-World Use Cases

  • Displaying database records
  • Processing form data
  • Generating tables dynamically
  • Looping through user lists
  • Menu-driven programs

Common Mistakes

  • Infinite loops due to wrong conditions
  • Forgetting increment/decrement
  • Using wrong loop type
  • Not using break when needed

Best Practices

  • Use for loop when count is known
  • Use while when condition-based execution is needed
  • Use foreach for arrays
  • Keep loop logic simple

🔗 Related Articles


Conclusion

PHP loops are essential for writing efficient and dynamic programs. By understanding for, while, do...while, and foreach loops, you can handle repetitive tasks easily.

Practice these examples and try modifying them to improve your skills. Bookmark this guide and explore related topics to strengthen your PHP fundamentals.

Comments