Working with Loops in PHP
Loops allow you to iterate over arrays and perform repetitive operations efficiently. Instead of writing 100 lines of code to process 100 items, you write one loop that repeats 100 times.
Types of Loops in PHP
Section titled “Types of Loops in PHP”1. The foreach Loop
Section titled “1. The foreach Loop”The foreach loop is ideal for arrays, automatically iterating through each element without manual index management.
Indexed Arrays
Section titled “Indexed Arrays”$fruits = ['Apple', 'Banana', 'Orange', 'Mango'];
foreach ($fruits as $fruit) {    echo $fruit . "\n";}
// Output:// Apple// Banana// Orange// MangoAssociative Arrays
Section titled “Associative Arrays”Access both keys and values using $key => $value syntax:
$student = [    'name' => 'John Doe',    'age' => 20,    'major' => 'Computer Science',    'gpa' => 3.8];
foreach ($student as $key => $value) {    echo "$key: $value\n";}
// Output:// name: John Doe// age: 20// major: Computer Science// gpa: 3.8Practical Example: Shopping Cart
Section titled “Practical Example: Shopping Cart”$cart = [    'laptop' => 999.99,    'mouse' => 25.50,    'keyboard' => 75.00,    'monitor' => 299.99];
$total = 0;
foreach ($cart as $item => $price) {    echo "$item: $" . number_format($price, 2) . "\n";    $total += $price;}
echo "Total: $" . number_format($total, 2);
// Output:// laptop: $999.99// mouse: $25.50// keyboard: $75.00// monitor: $299.99// Total: $1400.482. The for Loop
Section titled “2. The for Loop”Use when you need index positions or precise iteration control.
$colors = ['Red', 'Green', 'Blue', 'Yellow'];
for ($i = 0; $i < count($colors); $i++) {    echo "Color $i: $colors[$i]\n";}
// Output:// Color 0: Red// Color 1: Green// Color 2: Blue// Color 3: YellowExample: Numbered List
$tasks = ['Read chapter 5', 'Complete assignment', 'Study for quiz'];
for ($i = 0; $i < count($tasks); $i++) {    echo ($i + 1) . ". $tasks[$i]\n";}// Output: 1. Read chapter 5 ...3. The while Loop
Section titled “3. The while Loop”Continues while a condition is true—useful when iteration count is unknown.
$numbers = [1, 2, 3, 4, 5];$index = 0;
while ($index < count($numbers)) {    echo $numbers[$index] . "\n";    $index++;}4. The do-while Loop
Section titled “4. The do-while Loop”Executes at least once since the condition is checked after the first iteration.
$index = 0;$fruits = ['Apple', 'Banana'];
do {    echo $fruits[$index] . "\n";    $index++;} while ($index < count($fruits));Multi-Dimensional Arrays
Section titled “Multi-Dimensional Arrays”Use nested loops to process arrays within arrays.
Example: Student Grades
$students = [    'Alice' => [        'Math' => 95,        'English' => 88,        'Science' => 92    ],    'Bob' => [        'Math' => 78,        'English' => 85,        'Science' => 80    ],    'Charlie' => [        'Math' => 90,        'English' => 92,        'Science' => 88    ]];
foreach ($students as $name => $grades) {    echo "$name's Grades:\n";
    $total = 0;    $count = 0;
    foreach ($grades as $subject => $score) {        echo "  $subject: $score\n";        $total += $score;        $count++;    }
    $average = $total / $count;    echo "  Average: " . number_format($average, 2) . "\n\n";}Loop Control Statements
Section titled “Loop Control Statements”Exits the loop immediately when a condition is met:
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
foreach ($numbers as $num) {    if ($num > 5) {        break; // Stop when we reach a number greater than 5    }    echo $num . " ";}// Output: 1 2 3 4 5continue
Section titled “continue”Skips the current iteration and moves to the next:
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
foreach ($numbers as $num) {    if ($num % 2 == 0) {        continue; // Skip even numbers    }    echo $num . " ";}// Output: 1 3 5 7 9Common Array Operations
Section titled “Common Array Operations”Filtering
Section titled “Filtering”$scores = [45, 78, 92, 65, 88, 34, 95];$passing_scores = [];
foreach ($scores as $score) {    if ($score >= 60) {        $passing_scores[] = $score;    }}
print_r($passing_scores);// Output: [78, 92, 65, 88, 95]Transformation
Section titled “Transformation”$prices = [10, 20, 30, 40, 50];$discounted_prices = [];
foreach ($prices as $price) {    $discounted_prices[] = $price * 0.8; // 20% discount}
print_r($discounted_prices);// Output: [8, 16, 24, 32, 40]Finding Min/Max
Section titled “Finding Min/Max”$temperatures = [72, 68, 75, 80, 65, 70];$max_temp = $temperatures[0];$min_temp = $temperatures[0];
foreach ($temperatures as $temp) {    if ($temp > $max_temp) {        $max_temp = $temp;    }    if ($temp < $min_temp) {        $min_temp = $temp;    }}
echo "Highest: $max_temp°F\n";echo "Lowest: $min_temp°F\n";Searching
Section titled “Searching”$products = [    ['id' => 1, 'name' => 'Laptop', 'price' => 999],    ['id' => 2, 'name' => 'Mouse', 'price' => 25],    ['id' => 3, 'name' => 'Keyboard', 'price' => 75]];
$search_id = 2;$found = null;
foreach ($products as $product) {    if ($product['id'] == $search_id) {        $found = $product;        break;    }}
if ($found) {    echo "Found: {$found['name']} - $" . $found['price'];}Best Practices
Section titled “Best Practices”- 
Choose the right loop: Use foreachwithout index needs,forwhen you need indices.
- 
Don’t modify arrays during iteration: Avoid adding/removing elements while looping. 
- 
Use descriptive variable names: Prefer $studentover$iin foreach loops.
- 
Cache count()in for loops: Callingcount()every iteration is inefficient.
// Less efficientfor ($i = 0; $i < count($array); $i++) { }
// Better$length = count($array);for ($i = 0; $i < $length; $i++) { }- Prevent infinite loops: Always ensure your loop condition can become false.
// BAD - Infinite loopwhile ($i < 10) {    echo $i;  // Forgot to increment!}
// GOODwhile ($i < 10) {    echo $i;    $i++;}