Lesson 4 โ€ข Beginner

    Control Flow ๐Ÿ”€

    Control program logic with if/elseif/else, ternary operators, switch statements, and PHP 8's match expression.

    What You'll Learn in This Lesson

    • โ€ข If / elseif / else for branching logic
    • โ€ข Nested conditions and combining with && and ||
    • โ€ข Ternary operator ? : and null coalescing ??
    • โ€ข Switch statement with break and intentional fall-through
    • โ€ข PHP 8's match expression โ€” the safer, cleaner switch

    1๏ธโƒฃ If / Elseif / Else Syntax

    <?php
    if ($condition1) {
        // runs if condition1 is true
    } elseif ($condition2) {
        // runs if condition2 is true
    } else {
        // runs if nothing above was true
    }
    ?>

    Key point: PHP uses elseif (one word) or else if (two words) โ€” both work, but elseif is more common.

    Try It: If / Elseif / Else

    Build a grading system, nested conditions, ternary operator, and combined logic

    Try it Yourself ยป
    JavaScript
    // PHP Control Flow: if / elseif / else (simulated in JavaScript)
    console.log("=== If / Elseif / Else ===");
    console.log();
    
    // Grading system
    let score = 78;
    console.log("Student score: " + score);
    console.log();
    
    if (score >= 90) {
        console.log("Grade: A โ€” Excellent! ๐ŸŒŸ");
    } else if (score >= 80) {
        console.log("Grade: B โ€” Good job! ๐Ÿ‘");
    } else if (score >= 70) {
        console.log("Grade: C โ€” Satisfactory โœ“");
    } else if (score >= 60) {
        console.log("Grade: D โ€” Needs improvement");
    } el
    ...

    Try It: Switch & Match

    Compare switch statement fall-through, PHP 8's match expression, and HTTP status codes

    Try it Yourself ยป
    JavaScript
    // PHP Switch Statement & Match Expression
    console.log("=== Switch Statement ===");
    console.log();
    console.log("Use switch when comparing one variable against many values.");
    console.log("Much cleaner than a long if/elseif chain!");
    console.log();
    
    let dayOfWeek = 3;  // 1=Mon, 7=Sun
    
    console.log("Day number: " + dayOfWeek);
    
    // PHP switch statement
    switch (dayOfWeek) {
        case 1:
            console.log("Monday โ€” Start of the week ๐Ÿ’ช");
            break;
        case 2:
            console.log("Tuesday โ€” Kee
    ...

    โš ๏ธ Common Mistakes

    โš ๏ธ
    Missing break in switch โ€” causes unintended fall-through to the next case.
    โš ๏ธ
    Using = instead of == โ€” if ($x = 5) assigns, not compares. Always use == or ===.
    โš ๏ธ
    Truthy/falsy confusion โ€” 0, "", null, "0", and empty arrays are all falsy in PHP.
    ๐Ÿ’ก
    Pro Tip: Prefer match() over switch in PHP 8+ โ€” no break needed, strict comparison by default, and it returns a value.

    ๐Ÿ“‹ Quick Reference โ€” Control Flow

    StructureUse When
    if/elseif/elseDifferent conditions to test
    ? :Simple one-line if/else
    ??Default value for null
    switchOne variable, many values
    match()Like switch but safer (PHP 8+)

    ๐ŸŽ‰ Lesson Complete!

    You've mastered PHP control flow! Next, learn how to repeat code with loops.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service