Advertisement

Google Ad Slot: content-top

PHP Loops


PHP supports several types of loops that allow you to execute a block of code repeatedly as long as a specified condition is true or for a fixed number of iterations. Here's an overview of the loops available in PHP:


  1. while loop: Repeats as long as a condition is true.
  2. do...while loop: Executes at least once, then continues as long as a condition is true.
  3. for loop: Used when the number of iterations is known.
  4. foreach loop: Used to iterate over elements in an array.


The following chapters will explain and give examples of each loop type.

Example
<?php
$i = 1;
while ($i <= 5) {
echo "Number: $i <br>";
$i++;
}
?>
Try it yourself