← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 15 of 24

PHP Match Expression Advanced

Advanced match strict lookup chart को ज़्यादा clever तरीकों से इस्तेमाल करता है, exact values के बजाय conditions पर match करने जैसे। यह अगली choice पर fall through होने के risk के बिना एक clean answer देता है।
Syntax
php
$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
<?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;
?>

कई Matches

आप एक single result share करने के लिए एक match arm पर कई comma-separated values list कर सकते हैं, जो हर fall-through case के लिए switch की माँग वाली repetition से बचाता है।

उदाहरण: Multiple Matches

php
<?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;
?>

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
<?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;
?>

default Match Fallback

एक default arm छोड़ने का मतलब है कि कोई arm की value subject से match न होते ही match एक UnhandledMatchError throw करता है, जो एक unmatched switch की तरह चुपचाप कुछ न करने के बजाय unexpected inputs को तुरंत surface करता है।

उदाहरण: The default Match Fallback

php
<?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();
}
?>

match के अंदर Exceptions Throw करना

क्योंकि match एक expression है, आप detect होते ही सही जगह एक invalid case reject करने के लिए एक arm के अंदर सीधे एक throw statement रख सकते हैं, validation logic को compact और बाकी arms के साथ colocated रखते हुए।

उदाहरण: Throwing Exceptions inside match

php
<?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();
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि match switch जैसा fall through करेगा, जबकि हर arm return करके रुक जाता है।
  2. एक default arm add न करना और किसी चीज़ से match न करने वाली values के लिए UnhandledMatchError पाना।
  3. यह भूल जाना कि match strict comparison इस्तेमाल करता है, इसलिए "1" 1 से match नहीं करता।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.