PHP loops


Loops in PHP are control structures that allow you to execute a block of code repeatedly based on certain conditions. They are useful for performing repetitive tasks and iterating over data structures. PHP provides several types of loops, each suited to different scenarios.

1. for Loop

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

Syntax:

for (initialization; condition; increment/decrement) { // Code to be executed repeatedly }
  • Initialization: Executes once at the beginning of the loop. Typically used to initialize a counter variable.
  • Condition: Evaluated before each iteration. If true, the loop continues; if false, the loop stops.
  • Increment/Decrement: Updates the counter variable after each iteration.

Example:

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

Explanation:

  • Initializes $i to 0.
  • Continues looping while $i is less than 5.
  • Increments $i by 1 each time through the loop.
  • Outputs numbers from 0 to 4.

2. while Loop

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

Syntax:

while (condition) { // Code to be executed repeatedly }
  • Condition: Evaluated before each iteration. If true, the loop continues; if false, the loop stops.

Example:

<?php $i = 0; while ($i < 5) { echo "Number: $i<br>"; $i++; } ?>

Explanation:

  • Initializes $i to 0.
  • Continues looping while $i is less than 5.
  • Outputs numbers from 0 to 4.
  • $i is incremented by 1 in each iteration.

3. do-while Loop

The do-while loop is similar to the while loop, but it guarantees that the block of code will be executed at least once before the condition is tested.

Syntax:

do { // Code to be executed repeatedly } while (condition);
  • Condition: Evaluated after each iteration. The loop continues as long as the condition is true.

Example:

<?php $i = 0; do { echo "Number: $i<br>"; $i++; } while ($i < 5); ?>

Explanation:

  • Initializes $i to 0.
  • Executes the block of code once before checking if $i is less than 5.
  • Continues looping and outputs numbers from 0 to 4.
  • $i is incremented by 1 in each iteration.

4. foreach Loop

The foreach loop is specifically designed for iterating over arrays. It simplifies the process of accessing each element in an array.

Syntax:

foreach ($array as $value) { // Code to be executed for each element }
  • $array: The array to be iterated over.
  • $value: The current element of the array in each iteration.

Example:

<?php $fruits = array("apple", "banana", "cherry"); foreach ($fruits as $fruit) { echo "Fruit: $fruit<br>"; } ?>

Explanation:

  • Iterates over each element in the $fruits array.
  • Outputs each fruit name: apple, banana, and cherry.

Associative Array Example:

<?php $person = array("name" => "John", "age" => 30, "city" => "New York"); foreach ($person as $key => $value) { echo "$key: $value<br>"; } ?>

Explanation:

  • Iterates over each key-value pair in the $person associative array.
  • Outputs each key and its corresponding value.

Summary

  • for Loop: Use when the number of iterations is known beforehand.
  • while Loop: Use when the number of iterations is not known and depends on a condition being true.
  • do-while Loop: Similar to while, but guarantees at least one execution.
  • foreach Loop: Ideal for iterating over arrays, including associative arrays.