Home / Journal

PHP (Part 4): Conditional Statements

PHP (Part 4): Conditional Statements

In Part 3 we learned how to compare and combine values with operators. The result of a comparison is always true or false — and that's exactly what we need to make our program make decisions.

That's what conditional statements are for. They let your code run one piece of logic when something is true, and a different piece when it isn't. Let me walk you through them.

The if Statement

The simplest decision. You give if a condition inside parentheses, and the code in the braces { } runs only if that condition is true:

<?php
    $age = 20;

    if ($age >= 18) {
        echo "You are an adult.";
    }
?>

Because $age is 20, the condition $age >= 18 is true, so the message prints. If $age were 15, the condition would be false and nothing would happen.

Adding an else

Usually you want a fallback — something to do when the condition is not true. That's what else is for:

<?php
    $age = 15;

    if ($age >= 18) {
        echo "You are an adult.";
    } else {
        echo "You are a minor.";
    }
?>

Here the condition is false, so the else block runs and we get "You are a minor." One of the two blocks always runs — never both.

Testing Several Cases with elseif

What if there are more than two possibilities? Chain them with elseif. PHP checks each condition from top to bottom and runs the first one that's true:

<?php
    $score = 82;

    if ($score >= 90) {
        echo "Grade: A";
    } elseif ($score >= 80) {
        echo "Grade: B";
    } elseif ($score >= 70) {
        echo "Grade: C";
    } else {
        echo "Grade: F";
    }
?>

A score of 82 fails the first test (>= 90) but passes the second (>= 80), so we get "Grade: B" — and PHP stops there, never checking the rest. The order matters: put your tightest conditions first.

Note: you can have as many elseif branches as you like, and the final else is optional.

Nesting Conditions

You can place an if inside another if when a decision depends on a previous one:

<?php
    $loggedIn = true;
    $isAdmin  = false;

    if ($loggedIn) {
        if ($isAdmin) {
            echo "Welcome, admin.";
        } else {
            echo "Welcome, user.";
        }
    } else {
        echo "Please log in.";
    }
?>

Nesting is fine in small doses, but if you find yourself three or four levels deep, it's usually a sign to rethink — often the logical operators (&&, ||) from Part 3 can flatten it out.

The switch Statement

When you're checking one variable against several fixed values, a long elseif chain gets repetitive. switch is cleaner for that:

<?php
    $day = "Tue";

    switch ($day) {
        case "Mon":
            echo "Start of the week.";
            break;
        case "Tue":
            echo "Taco Tuesday!";
            break;
        case "Fri":
            echo "Almost weekend.";
            break;
        default:
            echo "Just another day.";
    }
?>

A few things to notice:

  • Each case is one value to compare $day against.
  • The break is important — it tells PHP to stop once a match runs. Forget it, and execution "falls through" into the next case, which is a classic beginner bug.
  • default is the catch-all, like the final else. It runs when no case matches.

The Ternary Shortcut

For a simple "if this, then A, else B" you can write it all on one line with the ternary operator (? :). It's a compact version of if/else:

<?php
    $age = 20;

    $status = ($age >= 18) ? "adult" : "minor";
    echo $status;   // outputs: adult
?>

Read it as: "is $age >= 18? If yes, use "adult", otherwise "minor"." Handy for short assignments — but don't overuse it, because long ternaries get hard to read fast.

A Quick Word on Truthiness

PHP will happily evaluate values that aren't strictly true/false inside a condition. An empty string "", the number 0, and null all count as false; most other values count as true. So this works:

<?php
    $name = "";

    if ($name) {
        echo "Hello, $name!";
    } else {
        echo "No name given.";
    }
?>

The empty string is treated as false, so we get "No name given." It's a useful shortcut, but be aware of it — a 0 or an empty string slipping into a condition is a common source of surprises.

To Be Continued…

You can now make your programs branch and decide. Combined with the operators from Part 3, that's already enough to write genuinely useful logic. In Part 5 we'll cover Loopswhile, for, and foreach — so your code can repeat work without you copy-pasting it a hundred times. Stay tuned!

← All articles