0PricingLogin
PHP Academy · Lesson

Match Expression and Named Arguments

Replace verbose switch with match and use named arguments for clarity.

The match Expression

PHP 8.0 introduced match, a concise alternative to switch that uses strict equality (===) and returns a value.

match vs switch

Key differences: match uses strict comparison, has no fall-through, and is an expression (returns a value).

<?php
$status = 2;
$label = match($status) {
    1 => "active",
    2 => "inactive",
    3 => "banned",
    default => "unknown",
};
echo $label; // "inactive"

All lessons in this course

  1. Match Expression and Named Arguments
  2. Nullsafe Operator and Union Types
  3. Enumerations (Enums)
  4. Fibers: Cooperative Concurrency
← Back to PHP Academy