Loops in php
Loops are used to execute the same block of code repeatedly as long as a condition is true. php supports while, do...while, for, and foreach loops.
The while Loop
Video Explanation

Executes code as long as the condition is true:
<?php
$i = 1;
while ($i <= 5) {
echo "Count: $i <br>";
$i++;
}
// Output: Count: 1, Count: 2 ... Count: 5
?>
The do...while Loop
Video Explanation

Executes the code block once before checking the condition, then repeats while true:
<?php
$i = 1;
do {
echo "Number: $i <br>";
$i++;
} while ($i <= 5);
?>
Key difference: The do...while loop always runs at least once, even if the condition is false from the start.
The for Loop
Video Explanation

Used when you know in advance how many times to loop:
<?php
for ($i = 0; $i < 5; $i++) {
echo "Item $i <br>";
}
?>
Syntax:
for (initialization; condition; increment) {
// code
}
The foreach Loop
Video Explanation

Used to loop through arrays:
<?php
$fruits = ["Apple", "Banana", "Cherry"];
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
// Apple, Banana, Cherry
?>
foreach with key => value
<?php
$person = ["name" => "Alice", "age" => 30, "city" => "Delhi"];
foreach ($person as $key => $value) {
echo "$key: $value <br>";
}
// name: Alice
// age: 30
// city: Delhi
?>