EZ

Eduzan

Learning Hub

Eduzan
Eduzan / PHP

PHP Control Statement

Computer Science / PHP tutorial chapter - Published 2025-12-16 - PHP

PHP enables us to execute specific actions based on conditions, which may involve logical or comparative evaluations. Depending on the outcome of these conditions, either TRUE or FALSE, a corresponding action will be taken. Think of it like a two-way path: if a condition is met, proceed one way; otherwise, go the other way. PHP provides us with four key conditional statements to handle such logic:

  • if statement
  • if…else statement
  • if…elseif…else statement
  • switch statement

Let’s take a closer look at each of these:

1. if Statement: This statement evaluates a condition, and if it returns TRUE, the block of code inside the if clause is executed.

Syntax:

if (condition) {
    // If TRUE, execute this code
}

Example

<?php
$number = 25;

if ($number > 10) {
    echo "The number is greater than 10";
}
?>

Output:

The number is greater than 10

2. if…else Statement: We know that when a condition holds TRUE, the code inside the if block gets executed. But what happens if the condition is FALSE? That’s where else comes in. If the condition is TRUE, the if block is executed; otherwise, the else block runs.

if (condition) {
    // If TRUE, execute this code
} else {
    // If FALSE, execute this code
}
/

Example:

<?php
$temperature = -5;

if ($temperature >= 0) {
    echo "The temperature is above freezing";
} else {
    echo "The temperature is below freezing";
}
?>

Output:

The temperature is below freezing

3. if…elseif…else Statement: This structure allows for multiple if and else conditions. It is useful when we have more than two possible scenarios to check.

Syntax:

if (condition1) {
    // If TRUE, execute this code
} elseif (condition2) {
    // If TRUE, execute this code
} elseif (condition3) {
    // If TRUE, execute this code
} else {
    // If none are TRUE, execute this code
}

Output:

<?php
$month = "March";

if ($month == "January") {
    echo "Start of the year";
} elseif ($month == "March") {
    echo "It's March!";
} elseif ($month == "December") {
    echo "End of the year";
} else {
    echo "Just another month";
}
?>

Output:

It's March!

4. switch Statement: The switch statement provides an efficient way to compare an expression to multiple values (cases) and execute the matching case. The break keyword is used to prevent fall-through, and the default keyword handles cases where no match is found.

Syntax:

switch(expression) {
    case value1:
        // Code to be executed if expression == value1;
        break;
    case value2:
        // Code to be executed if expression == value2;
        break;
    ...
    default:
        // Code to be executed if no case matches
}

Example:

<?php
$day = "Tuesday";

switch($day) {
    case "Monday":
        echo "Start of the week!";
        break;
    case "Tuesday":
        echo "It's Tuesday";
        break;
    case "Wednesday":
        echo "Midweek";
        break;
    default:
        echo "Not a specific day";
}
?>

Output:

It's Tuesday
End of lesson.