PHP match Expression
In this page:
$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
// Declare `$status`, set to `2`
$status = 2;
$label = match($status) {
1 => 'Pending',
2 => 'Active',
3 => 'Closed',
};
// Print `$label` to the output
echo $label;
?>
Login to try C/C++/Java/PHP code in the editor
Comma से Values को Group करना
एक single match arm => से पहले comma-separated कई values एक साथ list करके उन्हें test कर सकता है — 1, 2, 3 => low उन तीनों values में से किसी के लिए same result चलाता है, तीन अलग arms की ज़रूरत के बिना।
उदाहरण: Grouping values with Comma
<?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;
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
- एक unlisted value के लिए
defaultarm छोड़ देना, जो एकUnhandledMatchErrorthrow करता है। - यह उम्मीद करना कि
matchfall through करेगा या प्रति arm कई statements चलाएगा, जबकि हर arm एक single expression है। - यह उम्मीद करना कि
matchswitchजैसे loosely compare करेगा, इसलिएmatch (1) { 1 => ... }match नहीं करता क्योंकि यह===इस्तेमाल करता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: