PHP Shorthand If
In this page:
$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
// Declare `$age`, set to `20`
$age = 20;
// Print `$age >= 18 ? "Adult" : "Minor"` to the output
echo $age >= 18 ? "Adult" : "Minor";
?>
Login to try C/C++/Java/PHP code in the editor
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
// Declare `$name`, set to ""
$name = "";
// Print `$name ?: "Anonymous"` to the output
echo $name ?: "Anonymous";
?>
Login to try C/C++/Java/PHP code in the editor
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
// Declare `$count`, set to `0`
$count = 0;
// Print `$count ?? "no value"` to the output
echo $count ?? "no value";
?>
Login to try C/C++/Java/PHP code in the editor
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
// Declare `$config` as an empty array
$config = [];
$config['theme'] ??= "light";
// Print `$config['theme']` to the output
echo $config['theme'];
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
- genuinely complex logic के लिए ternary operator को overuse करना, एक dense, hard-to-read one-liner produce करते हुए जहाँ एक normal if-else block असल में clearer होता।
- null coalescing operator (??) को ternary operator से confuse करना -- ?? specifically null/unset check करता है, जबकि ?: general truthiness check करता है।
- 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 अलग है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: