Control Flow in PHP: if, switch & Loops

    Atul Kabra4 min readUpdated

    Control Flow in PHP

    Control flow is how a program decides what to do and how many times to do it. PHP gives you conditional statements (if, switch, match) to make decisions and loops (for, while, foreach) to repeat work. These tools turn a flat list of statements into real logic.

    Making decisions with if / else

    The if statement runs a block of code only when a condition is true. Add elseif and else to handle more cases.

    <?php
    $marks = 72;
    
    if ($marks >= 75) {
        echo "Distinction";
    } elseif ($marks >= 60) {
        echo "First class";   // this runs, since 72 is >= 60
    } else {
        echo "Keep practising";
    }
    ?>
    

    The condition inside the parentheses must evaluate to true or false.

    The switch statement

    When you compare one value against many options, switch is cleaner than a long chain of if/elseif.

    <?php
    $day = "Tue";
    
    switch ($day) {
        case "Mon":
            echo "Start of the week";
            break;       // break stops the switch from "falling through"
        case "Tue":
            echo "Lab day";
            break;
        default:
            echo "Regular day";
    }
    ?>
    

    Always include break at the end of each case, or PHP will continue running the next case too.

    The match expression (PHP 8)

    match is a modern, safer alternative to switch. It returns a value, uses strict comparison, and needs no break.

    <?php
    $grade = "B";
    
    $message = match ($grade) {
        "A" => "Excellent",
        "B" => "Good",        // strict match, returns this value
        "C" => "Average",
        default => "Unknown grade",
    };
    
    echo $message; // Good
    ?>
    

    Loops: repeating work

    for loop

    Use for when you know how many times to repeat.

    <?php
    // Print numbers 1 to 5.
    for ($i = 1; $i <= 5; $i++) {
        echo $i . " ";
    }
    // Output: 1 2 3 4 5
    ?>
    

    while loop

    while repeats as long as a condition stays true.

    <?php
    $count = 1;
    
    while ($count <= 3) {
        echo "Attempt $count ";
        $count++;   // important: change the condition, or the loop never ends
    }
    ?>
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    do...while loop

    do...while runs the body at least once before checking the condition.

    <?php
    $n = 10;
    
    do {
        echo "This runs once even though the condition is false.";
    } while ($n < 5);
    ?>
    

    foreach loop

    foreach is the easiest way to loop over an array. It is the loop you will use most often in real PHP.

    <?php
    $students = ["Aarti", "Vivek", "Neha"];
    
    foreach ($students as $student) {
        echo $student . "<br>";
    }
    ?>
    

    You can also access the key (index) of each item:

    <?php
    $scores = ["Maths" => 88, "Science" => 91];
    
    foreach ($scores as $subject => $score) {
        echo "$subject: $score<br>";
    }
    ?>
    

    Break and continue

    • break exits a loop early.
    • continue skips to the next iteration.
    <?php
    for ($i = 1; $i <= 5; $i++) {
        if ($i === 3) {
            continue; // skip 3
        }
        if ($i === 5) {
            break;    // stop before printing 5
        }
        echo $i . " "; // prints 1 2 4
    }
    ?>
    

    Common mistakes

    • Forgetting break in a switch, causing every following case to run.
    • Creating an infinite loop by never changing the loop condition (forgetting $count++).
    • Using = instead of ==/=== inside an if, which assigns instead of compares.
    • Looping over an array with for and a wrong length, when foreach is simpler and safer.

    FAQ

    When should I use match instead of switch? Use match in PHP 8 when you want a value back and strict comparison. Use switch for older code or when running several statements per case.

    What is the difference between while and do...while? while may run zero times if the condition starts false. do...while always runs at least once.

    Which loop should I use for arrays? foreach is the clearest choice for arrays and avoids off-by-one index errors.

    Keep learning

    Back to the PHP & MySQL hub, revisit Operators in PHP, or continue to Arrays in PHP.

    Turn these basics into working features with mentor guidance. Join the waitlist for the PHP course at Infoplanet in Jalgaon.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides