Module 2 – PHP Fundamentals
Lesson
Switch
Multiple branches for one value
switch compares an expression against a list of case values. break prevents fall-through to the next cases. Since PHP 8, the match expression is a stricter alternative with a return value. Switch is suitable for menus, order statuses, and HTTP methods.
When to use
When you have 3+ branches for the same variable; for complex logic, if/elseif is often clearer.
<?php
$status = "paid";
switch ($status) {
case "paid": echo "Plaćeno"; break;
default: echo "Nepoznato";
}Example variation
Extend the lesson example: add another variable, change the loop boundary or branching, and observe the output. Deliberately introduce an error (e.g. an undefined variable) to see the PHP message – then fix it.
