Skip to main content

Course Progress

Loading...

PHP For Loops - Control Structures

Duration: 30 minutes
Module 2: Loops in PHP

Learning Objectives

  • Master PHP loop structures
  • Choose appropriate loop types
  • Control loop execution flow
  • Optimize loop performance

Introduction to PHP For Loops

Welcome to our session on PHP For Loops, an essential control structure for any PHP developer. Loops are fundamental programming constructs that allow us to execute a block of code repeatedly, making our programs more efficient and our code more concise.

Think of a for loop as a carousel at an amusement park - it goes around a specific number of times, following a predetermined path, and stops when the ride is complete. Similarly, a for loop executes a block of code a specified number of times until a condition is met.

Diagram
True False >|True| D[Execute Code Block] D >|False| F[Exit Loop] F Start Initialize Counter Execute Code Block Increment/Decrement Counter Exit Loop Continue Program Test Condition

Basic Syntax of PHP For Loops

The for loop in PHP follows a specific syntax with three main parts:

for (initialization; condition; increment/decrement) {
    // code to be executed
}
  • Initialization: Sets up the starting value for the loop counter variable (executed once at the beginning)
  • Condition: Evaluated at the beginning of each iteration - if true, the loop continues; if false, the loop ends
  • Increment/Decrement: Updates the counter variable after each iteration

Think of these three components as the preparation, the checkpoint, and the step forward in a journey. You prepare for the journey (initialization), check if you've reached your destination (condition), and take a step forward (increment/decrement).

Example: Basic For Loop

<?php
// Print numbers from 1 to 5
for ($i = 1; $i <= 5; $i++) {
    echo "Number: $i <br>";
}
?>

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

In this example, $i = 1 initializes the counter variable, $i <= 5 is the condition that keeps the loop running as long as $i is less than or equal to 5, and $i++ increments the counter by 1 after each iteration.

How For Loops Execute

Let's break down exactly how a for loop executes:

  1. The initialization expression is evaluated once when the loop begins.
  2. The condition is evaluated at the start of each iteration. If true, the loop body executes; if false, the loop terminates.
  3. After the loop body executes, the increment/decrement expression runs.
  4. Control returns to step 2, and the process repeats until the condition becomes false.
Initialization Condition Check Code Execution Increment/Decrement Exit when condition false

Think of a for loop as an assembly line in a factory. The initialization sets up the conveyor belt, the condition checks if there are still items to process, the code block performs operations on each item, and the increment/decrement moves the conveyor belt forward to the next item.

Variations and Techniques

Counting Backwards

For loops can count in any direction - commonly forwards, but they can also count backwards:

<?php
// Countdown from 10 to 1
for ($i = 10; $i >= 1; $i--) {
    echo "Countdown: $i <br>";
}
?>

Output:

Countdown: 10
Countdown: 9
Countdown: 8
...
Countdown: 1

This is like counting down for a rocket launch - we start at 10 and go down until we reach 1.

Custom Increments

You can increment or decrement by values other than 1:

<?php
// Print even numbers from 0 to 20
for ($i = 0; $i <= 20; $i += 2) {
    echo "Even number: $i <br>";
}
?>

Output:

Even number: 0
Even number: 2
Even number: 4
...
Even number: 20

Think of this as taking stairs two at a time - you're still ascending, but with bigger steps.

Multiple Initializations and Increments

You can use multiple expressions in the initialization and increment sections by separating them with commas:

<?php
// Using multiple variables in a for loop
for ($i = 0, $j = 10; $i < 10; $i++, $j--) {
    echo "i = $i, j = $j <br>";
}
?>

Output:

i = 0, j = 10
i = 1, j = 9
i = 2, j = 8
...
i = 9, j = 1

This is similar to having two trains moving in opposite directions along different tracks, with each iteration representing a checkpoint where we observe both trains' positions.

Empty Sections

Any of the three sections in a for loop can be empty, but the semicolons are still required:

<?php
// Initialize $i before the loop
$i = 1;
// Condition only
for (; $i <= 5; ) {
    echo "Number: $i <br>";
    $i++; // Increment inside the loop
}
?>

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

This example shows that the components of a for loop can be moved outside the loop definition, similar to how a chef might prepare some ingredients before starting a cooking procedure.

Practical Applications

Iterating Over Arrays

While foreach is typically preferred for arrays in PHP, a for loop can be used to iterate through indexed arrays:

<?php
$fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry"];

// Iterate through an array using a for loop
echo "<ul>";
for ($i = 0; $i < count($fruits); $i++) {
    echo "<li>Fruit #" . ($i + 1) . ": " . $fruits[$i] . "</li>";
}
echo "</ul>";
?>

Output:

<ul>
<li>Fruit #1: Apple</li>
<li>Fruit #2: Banana</li>
<li>Fruit #3: Cherry</li>
<li>Fruit #4: Date</li>
<li>Fruit #5: Elderberry</li>
</ul>

This is like walking through a garden row by row, looking at each plant and recording its details.

Performance Tip:

For better performance, calculate the array size once before the loop starts:

<?php
$fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry"];
$fruitCount = count($fruits);

for ($i = 0; $i < $fruitCount; $i++) {
    echo "Fruit #" . ($i + 1) . ": " . $fruits[$i] . "<br>";
}
?>

This is more efficient because PHP doesn't need to recalculate the array size at each iteration. It's like measuring the length of a road once before starting your journey, rather than measuring it at each step.

Generating HTML Tables

For loops are excellent for creating structured HTML content like tables:

<?php
// Generate a multiplication table
echo "<table border='1'>";

// Table header
echo "<tr><th>×</th>";
for ($i = 1; $i <= 10; $i++) {
    echo "<th>$i</th>";
}
echo "</tr>";

// Table body
for ($i = 1; $i <= 10; $i++) {
    echo "<tr>";
    echo "<th>$i</th>";
    
    for ($j = 1; $j <= 10; $j++) {
        echo "<td>" . ($i * $j) . "</td>";
    }
    
    echo "</tr>";
}

echo "</table>";
?>

Output:

This generates a 10×10 multiplication table.

× 1 2 3 ... 9 10 1 2 3 ... 9 10 1 2 2 4 ... 100

This example uses nested for loops - think of it as creating a grid of coordinates. The outer loop represents the row (like latitude), and the inner loop represents the column (like longitude). Together, they cover every cell in the table.

Dynamic Form Generation

For loops can create form elements dynamically:

<?php
// Generate a form with multiple selection options
echo '<form method="post">';
echo '<label>Select your favorite colors:</label><br>';

$colors = [
    'red' => 'Red',
    'blue' => 'Blue',
    'green' => 'Green',
    'yellow' => 'Yellow',
    'purple' => 'Purple',
    'orange' => 'Orange'
];

for ($i = 0; $i < count($colors); $i++) {
    $key = array_keys($colors)[$i];
    $value = array_values($colors)[$i];
    echo '<input type="checkbox" name="colors[]" value="' . $key . '"> ' . $value . '<br>';
}

echo '<input type="submit" value="Submit">';
echo '</form>';
?>

Output:

<form method="post">
<label>Select your favorite colors:</label><br>
<input type="checkbox" name="colors[]" value="red"> Red<br>
<input type="checkbox" name="colors[]" value="blue"> Blue<br>
<input type="checkbox" name="colors[]" value="green"> Green<br>
<input type="checkbox" name="colors[]" value="yellow"> Yellow<br>
<input type="checkbox" name="colors[]" value="purple"> Purple<br>
<input type="checkbox" name="colors[]" value="orange"> Orange<br>
<input type="submit" value="Submit">
</form>

Like a chef preparing identical appetizers for a party, this loop creates similar form elements for each color in our array.

Note:

While this example works, using a foreach loop would be more appropriate for associative arrays in PHP. We'll cover that in our next session.

Performance Considerations

Diagram
> C[Avoid Expensive Operations in Condition] A For Loop Performance Pre-calculate Array Length Avoid Expensive Operations in Condition Consider Alternative Loop Types Minimize Operations Inside Loop

Think about a for loop as a factory assembly line - the more you optimize each step, the more efficient the entire production line becomes:

  • Pre-calculate values outside the loop: If a value doesn't change during loop execution, calculate it once before the loop starts.
  • Avoid function calls in the condition: Function calls like count() in the condition section are evaluated on each iteration, causing performance issues in large loops.
  • Minimize work inside the loop: Keep the code inside the loop as light as possible - move any operations that don't need to be repeated outside the loop.
  • Consider alternatives: For some tasks, especially with arrays, a foreach loop might be more appropriate and efficient.

Inefficient vs. Efficient For Loop

Inefficient:

<?php
// Inefficient - count() called on each iteration
for ($i = 0; $i < count($largeArray); $i++) {
    // Do something with $largeArray[$i]
    echo $largeArray[$i] . "<br>";
}
?>

Efficient:

<?php
// Efficient - count() called only once
$size = count($largeArray);
for ($i = 0; $i < $size; $i++) {
    // Do something with $largeArray[$i]
    echo $largeArray[$i] . "<br>";
}
?>

This is like measuring all your ingredients once before cooking (efficient) versus measuring each ingredient every time you use it (inefficient).

Real-World Examples

Pagination System

For loops are perfect for creating pagination systems for displaying large sets of data:

<?php
// Pagination example
$totalItems = 235;  // Total number of items
$itemsPerPage = 20; // Items to display per page
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$totalPages = ceil($totalItems / $itemsPerPage);

// Generate pagination links
echo "<div class='pagination'>";

// Previous page link
if ($currentPage > 1) {
    echo "<a href='?page=" . ($currentPage - 1) . "'><< Prev</a> ";
}

// Page number links
for ($i = 1; $i <= $totalPages; $i++) {
    if ($i == $currentPage) {
        echo "<span class='current'>$i</span> ";
    } else {
        echo "<a href='?page=$i'>$i</a> ";
    }
}

// Next page link
if ($currentPage < $totalPages) {
    echo "<a href='?page=" . ($currentPage + 1) . "'>Next >></a>";
}

echo "</div>";
?>

Output (if current page is 3):

This pagination system is like a book's table of contents - it allows users to navigate through large amounts of content in manageable chunks.

Date Range Generator

For loops can generate ranges of dates for calendars or scheduling applications:

<?php
// Generate a range of dates (next 14 days)
$startDate = new DateTime(); // Today
$numDays = 14;

echo "<h3>Available appointment dates:</h3>";
echo "<ul>";

for ($i = 1; $i <= $numDays; $i++) {
    // Clone the start date and add days
    $date = clone $startDate;
    $date->modify("+$i days");
    
    // Skip weekends (Saturday = 6, Sunday = 0)
    $dayOfWeek = $date->format('w');
    if ($dayOfWeek == 0 || $dayOfWeek == 6) {
        continue; // Skip this iteration
    }
    
    $formattedDate = $date->format('l, F j, Y');
    echo "<li><a href='booking.php?date=" . $date->format('Y-m-d') . "'>$formattedDate</a></li>";
}

echo "</ul>";
?>

Notice the use of continue to skip weekend days. This is like planning a trip and skipping certain locations that don't interest you.

Common Patterns and Use Cases

Working with String Characters

For loops can process individual characters in a string:

<?php
// Count vowels in a string
$string = "Hello World! How are you today?";
$vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
$vowelCount = 0;

for ($i = 0; $i < strlen($string); $i++) {
    if (in_array($string[$i], $vowels)) {
        $vowelCount++;
    }
}

echo "The string contains $vowelCount vowels.";
?>

Output:

The string contains 9 vowels.

Performance Tip:

For better performance when working with strings, consider storing the string length before the loop:

<?php
$string = "Hello World! How are you today?";
$length = strlen($string);

for ($i = 0; $i < $length; $i++) {
    // Process character $string[$i]
}
?>

This is like a proofreader examining each letter in a document individually, looking for specific characters.

Number Series Generation

For loops excel at generating mathematical sequences:

<?php
// Generate Fibonacci sequence
$n = 10; // Number of terms to generate
$fibonacci = [];

// First two numbers are fixed
$fibonacci[0] = 0;
$fibonacci[1] = 1;

// Generate the rest using a for loop
for ($i = 2; $i < $n; $i++) {
    $fibonacci[$i] = $fibonacci[$i-1] + $fibonacci[$i-2];
}

echo "First $n numbers of the Fibonacci sequence: ";
for ($i = 0; $i < $n; $i++) {
    echo $fibonacci[$i];
    if ($i < $n - 1) {
        echo ", ";
    }
}
?>

Output:

First 10 numbers of the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

This example illustrates how for loops can implement mathematical algorithms. It's like a builder constructing each floor of a building using materials from the previous floors.

Best Practices and Pitfalls

Diagram
> C[Avoid Infinite Loops] A > E[Consider Performance Impact] B > B2[Foreach: Arrays/Collections] B > C1[Ensure condition becomes false] C > D1[Avoid modifying loop counter inside loop] D For Loop Best Practices Use Appropriate Loop Type Avoid Infinite Loops Be Careful with Loop Variables Consider Performance Impact For: Known iterations Foreach: Arrays/Collections While: Unknown iterations Ensure condition becomes false Check increment/decrement Avoid modifying loop counter inside loop Be mindful of variable scope Pre-calculate values Optimize operations inside loop

Best Practices

  • Choose the right loop: Use for loops when you know the exact number of iterations in advance.
  • Descriptive counter names: Use meaningful variable names for your counter (e.g., $row, $col) when appropriate, rather than always using $i, $j, etc.
  • Pre-calculate values: Calculate values that don't change during the loop execution before the loop starts.
  • Avoid modifying loop counters: Don't modify your loop counter variable inside the loop body, as this can lead to unexpected behavior.
  • Use proper code indentation: Properly indent your code to make it more readable and to clearly show which code is inside the loop.

Common Pitfalls

Infinite Loops

One of the most common issues is creating an infinite loop that never terminates:

<?php
// INCORRECT: Infinite loop
for ($i = 1; $i >= 0; $i++) {
    echo "This will run forever!";
}
?>

Since $i starts at 1 and keeps increasing, the condition $i >= 0 will always be true.

Infinite loops are like getting on a circular train track with no exits - you'll keep going around forever unless something interrupts the journey.

Off-by-One Errors

These are common when working with arrays or strings:

<?php
$array = ["Apple", "Banana", "Cherry", "Date", "Elderberry"];

// INCORRECT: Off-by-one error
for ($i = 0; $i <= count($array); $i++) {
    echo $array[$i] . "<br>"; // Error on last iteration!
}
?>

The condition should be $i < count($array) because array indices in PHP start at 0 and go up to count-1.

Performance Issues

Performing unnecessary operations inside loops can significantly impact performance:

<?php
// INEFFICIENT: Database query inside loop
for ($i = 0; $i < 1000; $i++) {
    $result = $db->query("SELECT * FROM products WHERE id = $i");
    // Process result
}
?>

This is inefficient because it makes a separate database query for each iteration. A better approach would be to fetch all required data with a single query and then process it:

<?php
// EFFICIENT: Single query outside loop
$ids = range(0, 999);
$result = $db->query("SELECT * FROM products WHERE id IN (" . implode(',', $ids) . ")");
$products = $result->fetch_all(MYSQLI_ASSOC);

for ($i = 0; $i < count($products); $i++) {
    // Process each product
}
?>

Advanced Techniques

Nested Loops

Nested loops are loops inside other loops, useful for working with multi-dimensional data:

<?php
// Create a simple star pattern
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    // Inner loop to print stars
    for ($j = 1; $j <= $i; $j++) {
        echo "* ";
    }
    echo "<br>";
}
?>

Output:

* 
* * 
* * * 
* * * * 
* * * * *

Nested loops are like exploring a multi-story building - the outer loop represents moving from floor to floor, while the inner loop represents examining each room on a floor.

Combination with Break and Continue

The break and continue statements can alter the flow of a for loop:

<?php
// Find the first prime number greater than 100
for ($num = 101; ; $num += 2) { // Note: no condition, using break instead
    $isPrime = true;
    
    // Check if $num is prime
    for ($i = 2; $i <= sqrt($num); $i++) {
        if ($num % $i == 0) {
            $isPrime = false;
            break; // Exit inner loop
        }
    }
    
    if ($isPrime) {
        echo "The first prime number greater than 100 is: $num";
        break; // Exit outer loop
    }
}
?>

Output:

The first prime number greater than 100 is: 101

This example shows an infinite for loop (no condition) that's terminated with a break statement when a prime number is found. It's like a treasure hunt where you stop searching as soon as you find what you're looking for.

Dynamic Loop Limits

Loop limits can be dynamic and change during execution:

<?php
// Process elements until a condition is met
$data = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50];
$sum = 0;
$limit = count($data);

for ($i = 0; $i < $limit; $i++) {
    $sum += $data[$i];
    
    // If sum exceeds 100, stop processing
    if ($sum > 100) {
        echo "Reached sum > 100 after processing " . ($i + 1) . " elements.<br>";
        echo "Final sum: $sum";
        break;
    }
}
?>

Output:

Reached sum > 100 after processing 5 elements.
Final sum: 105

This is like filling a bucket - you keep adding water until it reaches a certain level, then you stop, regardless of how much water you initially planned to add.

Practice Exercises

Exercise 1: Number Pyramid

Create a number pyramid pattern using nested for loops:

Expected Output:

    1
   222
  33333
 4444444
555555555

Hint:

You'll need two nested for loops - one for rows and one for columns. For each row, print spaces and then numbers.

Exercise 2: FizzBuzz

Write a program that prints numbers from 1 to 100, but for multiples of 3, print "Fizz" instead of the number, for multiples of 5, print "Buzz", and for multiples of both 3 and 5, print "FizzBuzz".

Hint:

Use a for loop with modulo operators (%) to check divisibility.

Exercise 3: Dynamic Calendar

Create a PHP script that displays a calendar for the current month using a for loop.

Hint:

Use PHP's date functions to get information about the current month, then use for loops to generate the calendar days.

Homework Assignment

Create a program that uses different types of loops

Develop a PHP application that demonstrates the use of for loops in at least three different scenarios:

  1. Create a simple calculator that generates a multiplication table for a user-provided number.
  2. Generate an HTML form with a select dropdown containing the years from 1900 to the current year.
  3. Create a pattern of your choice using nested for loops.

Requirements:

  • Include meaningful comments explaining your code.
  • Implement at least one performance optimization technique discussed in class.
  • Format your output with appropriate HTML and CSS.
  • Submit your code via the class GitHub repository by the next session.

Additional Resources

Coming Up Next

In our next session, we'll continue exploring control structures with while loops, do-while loops, and foreach loops. We'll also discuss how to choose the right loop type for different scenarios and delve deeper into loop control statements.

Make sure to complete the homework assignment to reinforce your understanding of for loops before the next class!