← Back to PHP Course | Chapter 4: Control Flow | Lesson 8 of 14

PHP match Expression

match switch का एक tidy cousin है जो आपको एक answer वापस देता है, किसी lookup chart जैसा जहाँ आप अपनी row ढूँढते हैं और result पढ़ते हैं। यह strict है, इसलिए यह सिर्फ exactly same value से match करता है।
Syntax
php
$result = match($value) {
    value1 => result1,
    value2, value3 => result2,
    default => default_result,
};

Basic match Expression

PHP 8.0 में add किया गया match, एक value को arms के एक set के against compare करता है और जो भी match करे उसका result *return* करता है — आप इसे सीधे assign करते हैं ($label = match($status) {...}) बजाय हर branch में अलग echo/assignment statements लिखने के, जैसे switch को चाहिए।

उदाहरण: Basic match Expression

php
<?php
// Declare `$status`, set to `2`
$status = 2;
$label = match($status) {
    1 => 'Pending',
    2 => 'Active',
    3 => 'Closed',
};
// Print `$label` to the output
echo $label;
?>

Comma से Values को Group करना

एक single match arm => से पहले comma-separated कई values एक साथ list करके उन्हें test कर सकता है — 1, 2, 3 => low उन तीनों values में से किसी के लिए same result चलाता है, तीन अलग arms की ज़रूरत के बिना।

उदाहरण: Grouping values with Comma

php
<?php
// Declare `$quantity`, set to `2`
$quantity = 2;
$level = match($quantity) {
    1, 2, 3 => 'low',
    4, 5, 6 => 'medium',
};
// Print `$level` to the output
echo $level;
?>

Strict Type Comparison

switch loose comparison (==) इस्तेमाल करता है, इसलिए 0 == 0 अनपेक्षित रूप से match कर सकता है।

match हमेशा strict semantics (===) से compare करता है, इसलिए एक string 1 कभी गलती से एक integer 1 से match नहीं करेगी — यह subtle bugs की एक पूरी category बंद कर देता है जिसके switch का prone है।

उदाहरण: Strict Type Comparison

php
<?php
// Declare `$value`, set to '0'
$value = '0';
$result = match(true) {
    $value === 0 => 'integer zero',
    $value === '0' => 'string zero',
    default => 'other',
};
// Print `$result` to the output
echo $result;
?>

default Fallback Case

match को हर possible input handle होना ज़रूरी है — अगर कोई value किसी arm से match नहीं करती और कोई default नहीं है, PHP चुपचाप कुछ न करने के बजाय एक UnhandledMatchError throw करता है।

यह एक deliberate design choice है: बिना notice किए slip करने वाले किसी case के बजाय, एक missing branch एक hard error बन जाता है जिसे आप तुरंत catch कर सकते हैं।

उदाहरण: The default Fallback Case

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',
        2 => 'Two',
    };
// Catch \UnhandledMatchError $e
} catch (\UnhandledMatchError $e) {
    // Print `"No arm matched: " . $e->getMessage()` to the output
    echo "No arm matched: " . $e->getMessage();
}
?>

match में Complex Conditions

match की जा रही value की तरह true pass करना, फिर arm conditions की तरह boolean expressions लिखना, match को सिर्फ fixed values के बजाय arbitrary comparisons evaluate करने देता है — effectively इसे एक elseif ladder के लिए एक strict, expression-based replacement में बदल देता है।

उदाहरण: Complex Conditions in match

php
<?php
// Declare `$age`, set to `25`
$age = 25;
$category = match(true) {
    $age < 13 => 'child',
    $age < 20 => 'teen',
    default => 'adult',
};
// Print `$category` to the output
echo $category;
?>
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. एक unlisted value के लिए default arm छोड़ देना, जो एक UnhandledMatchError throw करता है।
  2. यह उम्मीद करना कि match fall through करेगा या प्रति arm कई statements चलाएगा, जबकि हर arm एक single expression है।
  3. यह उम्मीद करना कि match switch जैसे loosely compare करेगा, इसलिए match (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.