🐘 PHP Beginner

What is the match expression in PHP 8?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for PHP development. It reveals whether you understand the building blocks that more complex concepts rely on.

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.

Common Mistake

Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex PHP answers easy to follow.