← Back to Curriculum

Control Structures

šŸ“š Lesson 3 of 12 ā±ļø 30 min

Control Structures

30 min

Control structures in PHP enable you to control the flow of program execution based on conditions and loops. PHP provides standard control structures found in most programming languages: conditional statements (if-else, switch), loops (for, while, do-while, foreach), and control flow statements (break, continue, return). Understanding control structures is fundamental to writing any PHP program that makes decisions or repeats actions.

Conditional statements allow your program to make decisions. The `if-else` statement executes code based on boolean conditions. You can chain multiple conditions with `elseif`. The `switch` statement provides a cleaner way to handle multiple conditions based on a single value. PHP 8.0+ introduced `match` expressions, which are more powerful than switch (they return values and use strict comparison). Understanding conditionals enables you to write programs that respond to different situations.

Loops enable you to repeat code execution. The `for` loop is ideal when you know how many iterations you need. The `while` loop continues as long as a condition is true. The `do-while` loop executes at least once, then checks the condition. The `foreach` loop is particularly powerful in PHP for iterating over arrays and objects—it's the most common loop for array processing. Understanding different loop types helps you choose the right one for each situation.

The `foreach` loop is PHP's most elegant way to iterate over arrays and objects. It can iterate over indexed arrays (values only) or associative arrays (keys and values). For objects, it iterates over public properties. The `foreach` loop is more readable and less error-prone than traditional for loops with array indices. Understanding foreach is essential for effective PHP array processing.

Control flow statements modify loop and function execution. `break` exits a loop or switch statement immediately. `continue` skips the rest of the current loop iteration and continues with the next. `return` exits a function and optionally returns a value. These statements provide fine-grained control over program flow. Understanding when to use each helps you write efficient, readable code.

Best practices include using `foreach` for array iteration (more readable and efficient), using `match` expressions for multiple conditions (PHP 8.0+), avoiding deeply nested conditionals (use early returns or guard clauses), and choosing the right loop type for each situation. Control structures should be clear, readable, and avoid unnecessary complexity. Understanding control structures enables you to write effective, maintainable PHP code.

Key Concepts

  • Control structures control program flow (conditionals and loops).
  • if-else statements execute code based on conditions.
  • Loops (for, while, foreach) repeat code execution.
  • foreach loop is ideal for iterating over arrays and objects.
  • break, continue, and return modify control flow.

Learning Objectives

Master

  • Using if-else and switch statements for conditional execution
  • Working with different loop types (for, while, foreach)
  • Understanding when to use each control structure
  • Using break, continue, and return effectively

Develop

  • Control flow thinking and program logic
  • Understanding when to use different loop types
  • Writing clear, maintainable control structures

Tips

  • Use foreach for array iteration—it's more readable and efficient.
  • Use match expressions (PHP 8.0+) instead of switch when possible.
  • Avoid deeply nested conditionals—use early returns or guard clauses.
  • Use break and continue to control loop execution when needed.

Common Pitfalls

  • Using for loops with array indices when foreach would be simpler.
  • Creating deeply nested conditionals that are hard to read.
  • Forgetting break statements in switch cases, causing fall-through.
  • Using wrong loop type, making code less readable or efficient.

Summary

  • Control structures enable conditional execution and loops.
  • if-else and switch handle conditional logic.
  • Loops (for, while, foreach) repeat code execution.
  • foreach is ideal for array and object iteration.
  • Understanding control structures is fundamental to PHP programming.

Exercise

Write a PHP script that demonstrates different control structures and loops.

<?php
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$fruits = ["apple" => "red", "banana" => "yellow", "grape" => "purple"];

echo "<h3>For Loop:</h3>";
for ($i = 0; $i < count($numbers); $i++) {
    echo $numbers[$i] . " ";
}

echo "<h3>Foreach Loop (Indexed Array):</h3>";
foreach ($numbers as $number) {
    if ($number % 2 == 0) {
        echo $number . " (even) ";
    } else {
        echo $number . " (odd) ";
    }
}

echo "<h3>Foreach Loop (Associative Array):</h3>";
foreach ($fruits as $fruit => $color) {
    echo ucfirst($fruit) . " is " . $color . "<br>";
}

echo "<h3>While Loop:</h3>";
$i = 0;
while ($i < 5) {
    echo "Count: " . $i . "<br>";
    $i++;
}

echo "<h3>Switch Statement:</h3>";
$day = "Monday";
switch ($day) {
    case "Monday":
        echo "Start of the week";
        break;
    case "Friday":
        echo "TGIF!";
        break;
    default:
        echo "Regular day";
}
?>

Code Editor

Output