← Back to blog
9/19/2025

PHP Control Structures – If, Loops, and Switch

Control structures are the building blocks that give your PHP scripts decision-making power and repetitive execution ability. Without them, programs w...

Control structures are the building blocks that give your PHP scripts decision-making power and repetitive execution ability. Without them, programs would simply execute sequentially, line by line, without any conditional behavior. In this post, we’ll cover the essentials of PHP control structures: conditionals (if, else, elseif, switch) and loops (while, do…while, for, and foreach).


1. The Role of Control Structures

Programs often need to make decisions and perform repetitive tasks:

  • Decisions: Should this code run or not?
  • Repetition: Run this block of code multiple times until a condition is met.

PHP provides built-in control structures to handle these cases efficiently.


2. Conditional Statements

2.1 The if Statement

The if statement evaluates a condition and executes code if the condition is true.

<?php
$age = 20;

if ($age >= 18) {
    echo "You are eligible to vote.";
}
?>

2.2 The if...else Statement

When you want an alternative action if the condition is false, use else.

<?php
$age = 16;

if ($age >= 18) {
    echo "You are eligible to vote.";
} else {
    echo "Sorry, you are too young to vote.";
}
?>

2.3 The if...elseif...else Statement

For multiple conditions, use elseif.

<?php
$marks = 75;

if ($marks >= 90) {
    echo "Grade: A";
} elseif ($marks >= 75) {
    echo "Grade: B";
} elseif ($marks >= 50) {
    echo "Grade: C";
} else {
    echo "Grade: F";
}
?>

3. The switch Statement

The switch statement is useful when you are comparing the same variable against different values.

<?php
$day = "Monday";

switch ($day) {
    case "Monday":
        echo "Start of the week!";
        break;
    case "Friday":
        echo "Weekend is near!";
        break;
    case "Sunday":
        echo "Time to rest!";
        break;
    default:
        echo "Just another day.";
}
?>

Note: Always include a break statement to prevent fall-through (unintentional execution of multiple cases).


4. Looping Structures

Loops allow code to run repeatedly until a condition is met.

4.1 The while Loop

Executes as long as the condition remains true.

<?php
$count = 1;

while ($count <= 5) {
    echo "Number: $count <br>";
    $count++;
}
?>

4.2 The do...while Loop

Executes the code block at least once, then checks the condition.

<?php
$count = 1;

do {
    echo "Number: $count <br>";
    $count++;
} while ($count <= 5);
?>

4.3 The for Loop

Best used when the number of iterations is known.

<?php
for ($i = 1; $i <= 5; $i++) {
    echo "Iteration: $i <br>";
}
?>

4.4 The foreach Loop

Specifically designed for arrays.

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

foreach ($fruits as $fruit) {
    echo "I like $fruit <br>";
}
?>

5. Nesting Control Structures

You can nest conditionals and loops to create more complex logic.

<?php
for ($i = 1; $i <= 3; $i++) {
    if ($i % 2 == 0) {
        echo "$i is even <br>";
    } else {
        echo "$i is odd <br>";
    }
}
?>

6. Practical Example – Multiplication Table

<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= 5; $j++) {
        echo ($i * $j) . " ";
    }
    echo "<br>";
}
?>

This generates a simple multiplication table.


7. Conclusion

Control structures are essential in PHP programming, enabling decision-making and repetition. With conditionals (if, else, elseif, switch) and loops (while, do…while, for, foreach), you can handle almost any logical flow in your applications.

In the next post, we’ll move deeper into PHP Functions, learning how to organize reusable blocks of code effectively.