Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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
Advertisements
