
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Match Expression in PHP 8
Match expression is a new feature that is added in PHP 8. It is very much similar to switch-case statements, but it provides more safe semantics.
Match expression does not use the 'case and break' structure of switch-case statements. It supports joint conditions, and it returns a value rather than entering a new code block.
We can store match results in a variable because it is an expression.
Match expression does not need a break statement like a switch. It supports only single-line expression.
Example: PHP 7 Using Switch Statement
<?php switch (1.0) { case '1.0': $result = "Hello World!"; break; case 1.0: $result = "Looks good"; break; } echo $result; ?>
Output
Hello World!
Example: Above PHP 7 Code Using PHP 8 Match Expression
<?php echo match (1.0) { '1.0' => "Hello World!", 1.0 => "Looks Good!", }; ?>
Output
Looks Good!
Example: Using PHP 8 Match Expression
<?php echo match (2) { 1 => 'Company', 2 => 'Department', 3 => 'Employee', }; ?>
Output
Employee
- Related Articles
- PHP – Match regular expression using mb_ereg_match()
- Regular expression to match numbers only in JavaScript?
- How to match parentheses in Python regular expression?
- Lambda expression in Java 8
- Explain Python regular expression search vs match
- MongoDB regular expression to match a specific record?
- How to put variable in regular expression match with JavaScript?
- How to match a word in python using Regular Expression?
- How to match a whitespace in python using Regular Expression?
- How to match only digits in Python using Regular Expression?
- How to use regular expression in Java to pattern match?
- Attributes in PHP 8
- How to match a regular expression against a string?
- How to match digits using Java Regular Expression (RegEx)
- How to match a nonwhitespace character in python using Regular Expression?

Advertisements