match
Last updated
What is match?
Match expressions are an integral feature of PHP, introduced in PHP 8.0 as an alternative to the switch statement. Compared to the switch statement, match expressions are safer since they don't require break statements and are more concise. The match expression can be an excellent tool for pattern matching. Here's an example:
$message = match ($statusCode) {
200, 300 => 'OK',
400 => 'error',
default => 'unknown status code',
};
In this code, based on the value of $statusCode, the match expression assigns a specific text to the $message. If $statusCode is not 200, 300, or 400, the default case applies. After running the code, the $message variable contains the result of the match expression.
Free resources to learn match
- Official docsmatch
More in stage 4: Conditionals
match: common questions
Can I learn match in Hindi?
This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain match in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.
Is this match tutorial for beginners?
It is lesson 25 of 120 in the free PHP roadmap, in stage 4, Conditionals. If you are new to PHP, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.