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

PHP Shorthand If

एक पूरा if-else block एक simple choice के लिए भी कई lines लेता है, जैसे किसी variable को assign करने के लिए दो short values में से चुनना। PHP exactly इस situation के लिए दो compact shorthand forms देता है: ternary operator (condition ? valueIfTrue : valueIfFalse) और null coalescing operator (value ?? fallback), दोनों एक छोटा decision एक single line में express करते हैं।
Syntax
php
$result = condition ? value_if_true : value_if_false;
$result = $value ?: $fallback;
$result = $value ?? $fallback;

Basic Ternary Operator

condition ? valueIfTrue : valueIfFalse condition evaluate करता है, और अगर यह truthy है तो valueIfTrue return करता है नहीं तो valueIfFalse -- सिर्फ एक value assign करने वाले चार-line के if-else block को एक single line में compress करते हुए।

उदाहरण: The Basic Ternary Operator

php
<?php
// Declare `$age`, set to `20`
$age = 20;
// Print `$age >= 18 ? "Adult" : "Minor"` to the output
echo $age >= 18 ? "Adult" : "Minor";
?>

Shorthand Ternary (Elvis Operator)

condition ?: valueIfFalse एक shorter form है जो middle value को दोहराना skip कर देता है -- यह अगर condition truthy है तो condition खुद return करता है, या अगर condition falsy है तो valueIfFalse।

यह खासतौर पर किसी variable की खुद की value truthy होने पर उसे चुनने, या नहीं तो एक fallback चुनने के लिए handy है।

उदाहरण: The Shorthand Ternary (Elvis Operator)

php
<?php
// Declare `$name`, set to ""
$name = "";
// Print `$name ?: "Anonymous"` to the output
echo $name ?: "Anonymous";
?>

Null Coalescing Operator (??)

value ?? fallback सिर्फ तभी fallback return करता है जब value null या पूरी तरह undefined हो (एक unset variable या missing array key), undefined case के लिए कोई warning raise किए बिना -- shorthand ternary के उलट, यह 0, "", या false जैसी दूसरी falsy values पर trigger नहीं होता।

उदाहरण: The Null Coalescing Operator (??)

php
<?php
// Declare `$count`, set to `0`
$count = 0;
// Print `$count ?? "no value"` to the output
echo $count ?? "no value";
?>

Null Coalescing Assignment Operator (??=)

$variable ??= $fallback सिर्फ तभी $variable को $fallback assign करता है जब $variable इस समय null या unset हो, नहीं तो इसे untouched छोड़ देता है -- एक explicit if statement के बिना "सिर्फ missing होने पर एक default भरना" का एक compact तरीका।

उदाहरण: The Null Coalescing Assignment Operator (??=)

php
<?php
// Declare `$config` as an empty array
$config = [];
$config['theme'] ??= "light";
// Print `$config['theme']` to the output
echo $config['theme'];
?>

Shorthand If से कब बचें

Shorthand forms एक single value decision के लिए बढ़िया हैं, लेकिन जैसे ही किसी branch को एक से ज़्यादा statement चलाना हो, कुछ log करना हो, या कई functions call करने हों, एक regular if-else block ज़्यादा clear और maintain करना आसान है -- shorthand syntax को code को बिना समझना मुश्किल किए छोटा बनाना चाहिए।

उदाहरण: When to Avoid Shorthand If

php
<?php
// Declare `$isAdmin`, set to `true`
$isAdmin = true;
// Check whether `$isAdmin`
if ($isAdmin) {
    // Print "Logging admin access\n" to the output
    echo "Logging admin access\n";
    // Print "Welcome, admin!" to the output
    echo "Welcome, admin!";
// Otherwise, run this branch
} else {
    // Print "Access denied" to the output
    echo "Access denied";
}
?>
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. genuinely complex logic के लिए ternary operator को overuse करना, एक dense, hard-to-read one-liner produce करते हुए जहाँ एक normal if-else block असल में clearer होता।
  2. null coalescing operator (??) को ternary operator से confuse करना -- ?? specifically null/unset check करता है, जबकि ?: general truthiness check करता है।
  3. parentheses के बिना कई ternary operators को chain करना, जो confusing या unintended grouping produce कर सकता है।
चैप्टर सारांश
  • Ternary operator, condition ? valueIfTrue : valueIfFalse, एक simple if-else को एक expression में compress करता है।
  • Shorthand ternary, condition ?: valueIfFalse, condition truthy होने पर condition खुद return करता है, repeated middle value skip करते हुए।
  • Null coalescing operator, value ?? fallback, specifically तब fallback return करता है जब value null या undefined हो, जो general truthiness से subtly अलग है।

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.