PHP Loops

Loops are used to execute a block of code repeatedly.

Types of Loops

PHP supports several types of loops:

  • For Loop: Used to execute a block of code for a specified number of times.
  • While Loop: Used to execute a block of code while a condition is true.
  • Do-While Loop: Used to execute a block of code while a condition is true.
  • Foreach Loop: Used to execute a block of code for each element in an array.

Example of PHP Loops

<?php
for ($i = 0; $i < 5; $i++) {
    echo "The number is $i\n";
}
?>

Output:

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4

Explanation: This example demonstrates the use of the for loop in PHP. The loop executes 5 times, and the value of $i is printed each time.