0PricingLogin
PHP Academy · Lesson

Switch and Match Statements

Use switch and PHP 8 match for multi-branch logic.

Switch vs if Chains

When you have many branches based on a single variable's value, switch is cleaner than a long if/elseif chain.

PHP 8 introduced match as a modern, strict alternative.

Switch Statement Basics

Switch evaluates its expression once and runs the matching case block:

<?php
$day = 'Monday';

switch ($day) {
    case 'Monday':
        echo 'Start of work week';
        break;
    case 'Friday':
        echo 'Almost weekend';
        break;
    default:
        echo 'Midweek';
}

All lessons in this course

  1. if, elseif, and else
  2. Switch and Match Statements
  3. for and while Loops
  4. foreach and Loop Control
← Back to PHP Academy