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
- Match Expression and Named Arguments
- Nullsafe Operator and Union Types
- Enumerations (Enums)
- Fibers: Cooperative Concurrency