Loops are fundamental control structures in programming that allow you to execute a block of code repeatedly. This repetition continues based on a certain condition or for a predetermined number of iterations. PHP supports several types of loops, each suited for different scenarios.
1. for Loop:
The `for` loop is used when you know in advance how many times you want to iterate. It's ideal for tasks where you need to perform an action a specific number of times.
* Syntax: `for (initialization; condition; increment/decrement) { code to be executed; }`
* Initialization: Executed once at the beginning of the loop. It typically initializes a counter variable.
* Condition: Evaluated before each iteration. If it evaluates to `true`, the loop continues; if `false`, the loop terminates.
* Increment/Decrement: Executed at the end of each iteration. It usually updates the counter variable.
2. while Loop:
The `while` loop executes a block of code as long as a specified condition is true. The condition is checked *before* each iteration. If the condition is initially false, the loop body will never be executed.
* Syntax: `while (condition) { code to be executed; }`
* Condition: Evaluated at the beginning of each loop pass. The loop continues as long as this condition remains `true`.
3. do-while Loop:
The `do-while` loop is similar to the `while` loop, but with one key difference: it guarantees that the loop's code block will be executed at least once. The condition is checked *after* the first execution of the loop body. If the condition is true, the loop continues; otherwise, it terminates.
* Syntax: `do { code to be executed; } while (condition);`
* Execution: The `do` block is executed first, then the `while` condition is evaluated.
4. foreach Loop:
The `foreach` loop is specifically designed to iterate over arrays and objects. It provides a simple way to process each element in a collection without needing to manage an index counter.
* Syntax (for values): `foreach ($array as $value) { code to be executed; }`
* Syntax (for keys and values): `foreach ($array as $key => $value) { code to be executed; }`
* Iteration: For each iteration, the value of the current array element is assigned to the `$value` variable (and optionally its key to the `$key` variable), and the internal array pointer is advanced.
Example Code
<?php
echo "<h1>PHP Loops Examples</h1>";
// 1. For Loop Example
echo "<h2>1. For Loop:</h2>";
for ($i = 0; $i < 5; $i++) {
echo "The number (for loop) is: " . $i . "<br>";
}
// 2. While Loop Example
echo "<h2>2. While Loop:</h2>";
$j = 0;
while ($j < 5) {
echo "The number (while loop) is: " . $j . "<br>";
$j++; // Important: increment the counter to avoid an infinite loop
}
// 3. Do-While Loop Example
echo "<h2>3. Do-While Loop:</h2>";
$k = 0;
do {
echo "The number (do-while loop) is: " . $k . "<br>";
$k++;
} while ($k < 5);
// Example showing do-while executes at least once
echo "<h3>Do-While (executes at least once, condition is false):</h3>";
$l = 10;
do {
echo "This will print once, even though \$l < 5 is false initially. Current \$l: " . $l . "<br>";
$l++;
} while ($l < 5);
// 4. Foreach Loop Example (indexed array)
echo "<h2>4. Foreach Loop (Indexed Array):</h2>";
$fruits = ["Apple", "Banana", "Cherry"];
foreach ($fruits as $fruit) {
echo "Fruit: " . $fruit . "<br>";
}
// 4. Foreach Loop Example (associative array with key and value)
echo "<h2>4. Foreach Loop (Associative Array):</h2>";
$person = [
"name" => "Alice",
"age" => 30,
"city" => "New York"
];
foreach ($person as $key => $value) {
echo ucfirst($key) . ": " . $value . "<br>";
}
?>








Loops (for, while, do-while, foreach)