PHP Match Expression Advanced
In this page:
$result = match(true) {
condition1 => result1,
condition2, condition3 => result2,
default => default_result,
};
match Expression क्या है?
match switch का एक stricter, ज़्यादा concise alternative है: यह एक expression है जो सीधे एक value return करता है, इसे break statements की ज़रूरत नहीं, और गलती से अगले case में fall through नहीं हो सकता।
उदाहरण: What is the match Expression?
<?php
// Declare `$grade`, set to `85`
$grade = 85;
$letter = match(true) {
$grade >= 90 => 'A',
$grade >= 80 => 'B',
default => 'C',
};
// Print `$letter` to the output
echo $letter;
?>
Login to try C/C++/Java/PHP code in the editor
कई Matches
आप एक single result share करने के लिए एक match arm पर कई comma-separated values list कर सकते हैं, जो हर fall-through case के लिए switch की माँग वाली repetition से बचाता है।
उदाहरण: Multiple Matches
<?php
// Declare `$day`, set to `6`
$day = 6;
$type = match($day) {
1, 2, 3, 4, 5 => 'Weekday',
6, 7 => 'Weekend',
};
// Print `$type` to the output
echo $type;
?>
Login to try C/C++/Java/PHP code in the editor
Strict Type Matching
switch की loose == comparison के उलट, match strict === semantics से compare करता है, value के साथ-साथ type भी check करते हुए -- इसलिए match(true) और 1 को 1 के against match करना एक switch statement से बहुत अलग behave करता है।
उदाहरण: Strict Type Matching
<?php
// Declare `$value`, set to '1'
$value = '1';
$result = match(true) {
$value === 1 => 'integer one',
$value === '1' => 'string one',
};
// Print `$result` to the output
echo $result;
?>
Login to try C/C++/Java/PHP code in the editor
default Match Fallback
एक default arm छोड़ने का मतलब है कि कोई arm की value subject से match न होते ही match एक UnhandledMatchError throw करता है, जो एक unmatched switch की तरह चुपचाप कुछ न करने के बजाय unexpected inputs को तुरंत surface करता है।
उदाहरण: The default Match Fallback
<?php
// Declare `$code`, set to `99`
$code = 99;
// Try running this block; jump to `catch` if it throws
try {
// Print `match($code) {` to the output
echo match($code) {
1 => 'One',
};
// Catch \UnhandledMatchError $e
} catch (\UnhandledMatchError $e) {
// Print `"Unhandled: " . $e->getMessage()` to the output
echo "Unhandled: " . $e->getMessage();
}
?>
Login to try C/C++/Java/PHP code in the editor
match के अंदर Exceptions Throw करना
क्योंकि match एक expression है, आप detect होते ही सही जगह एक invalid case reject करने के लिए एक arm के अंदर सीधे एक throw statement रख सकते हैं, validation logic को compact और बाकी arms के साथ colocated रखते हुए।
उदाहरण: Throwing Exceptions inside match
<?php
// Define the function `checkStatus` taking `$status`
function checkStatus($status) {
// Return `match($status) {` from this function
return match($status) {
'active', 'pending' => true,
default => throw new InvalidArgumentException("Invalid status: $status"),
};
}
// Try running this block; jump to `catch` if it throws
try {
// Call `checkStatus('deleted')`
checkStatus('deleted');
// Catch InvalidArgumentException $e
} catch (InvalidArgumentException $e) {
// Print `$e->getMessage()` to the output
echo $e->getMessage();
}
?>
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
matchswitchजैसा fall through करेगा, जबकि हर arm return करके रुक जाता है। - एक
defaultarm add न करना और किसी चीज़ से match न करने वाली values के लिएUnhandledMatchErrorपाना। - यह भूल जाना कि
matchstrict comparison इस्तेमाल करता है, इसलिए"1"1से match नहीं करता।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser