🐘 PHP Beginner

What is the match expression in PHP 8?

Answer

The match expression (PHP 8.0) is a more powerful and strict replacement for the switch statement. Key differences from switch: match uses strict comparison (===) instead of loose comparison (==), each branch returns a value (it is an expression), there is no fall-through between branches, and it throws an UnhandledMatchError if no branch matches (instead of silently doing nothing). Example: $result = match($status) { 1 => "active", 2 => "inactive", default => "unknown" };. Multiple conditions per branch: 1, 2 => "low value". Match makes code safer and more concise than switch.