Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 4 of 9

Control Structures - if/else, switch & match

Control structures direct the flow of execution. PHP provides if/elseif/else, switch/match, and a set of conditional expression shortcuts. Mastering early-return and guard clauses leads to cleaner, flatter code - avoiding deeply nested if/else pyramids.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

if/elseif/else & Guard Clauses

if/elseif/else & Guard Clauses
<?php
declare(strict_types=1);

// ── Standard if/else ───────────────────────────────────────────
$score = 85;

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

// ── Guard clause pattern (preferred) ──────────────────────────
// ❌ Nested - hard to read
function processOrder(array $order): string {
    if ($order !== []) {
        if (isset($order["user_id"])) {
            if ($order["total"] > 0) {
                return "Processing order #{$order['id']}";
            }
        }
    }
    return "Invalid order";
}

// ✅ Guard clauses - flat, easier to read
function processOrderClean(array $order): string {
    if (empty($order))            return "Empty order";
    if (!isset($order["user_id"])) return "Missing user";
    if ($order["total"] <= 0)      return "Invalid total";

    return "Processing order #{$order['id']}";
}

// ── Alternative syntax (useful in HTML templates) ───────────────
$isLoggedIn = true;
?>

<?php if ($isLoggedIn): ?>
    <nav><a href="/dashboard">Dashboard</a></nav>
<?php else: ?>
    <nav><a href="/login">Login</a></nav>
<?php endif; ?>

<?php
// ── is_* helper functions ──────────────────────────────────────
$value = null;
is_null($value);       // true
is_array([1,2]);       // true
is_numeric("42");      // true - "42" behaves like a number
is_numeric("42abc");   // false

// empty() - true for: null, false, 0, "", "0", [], unset vars
$input = "";
if (empty($input)) {
    echo "Input is empty";
}

// isset() - true if variable exists AND is not null
if (isset($_GET["page"])) {
    $page = (int) $_GET["page"];
}

Quick Quiz

Tip

Tip

Practice Control Structures ifelse switch match in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Control Structures - if/else, switch & match

Practice Task

Note

Practice Task - (1) Write a working example of Control Structures ifelse switch match from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Control Structures ifelse switch match is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/9 done