PHP Ternary Operator
In this page:
$result = condition ? value_if_true : value_if_false;
$result = $value ?: $fallback; // short ternary
$result = $value ?? $fallback; // null coalescing
Basic Ternary Operator
condition ? valueIfTrue : valueIfFalse एक simple two-way choice को एक single expression में pack करता है, एक 4-line if/else block की जगह लेते हुए जो सिर्फ एक variable assign करता है।
यह तब सबसे अच्छा पढ़ता है जब condition और दोनों outcomes छोटे और clear हों — ज़्यादा involved किसी भी चीज़ के लिए, एक real if/else ज़्यादा readable रहता है।
उदाहरण: Basic Ternary Operator
<?php
// Declare `$age`, set to `20`
$age = 20;
// Declare `$status`, set to `$age >= 18 ? "adult" : "minor"`
$status = $age >= 18 ? "adult" : "minor";
// Print `$status` to the output
echo $status;
?>
Login to try C/C++/Java/PHP code in the editor
Short Ternary Operator
$value ?: $fallback — short ternary, जिसे कभी-कभी इसके दिखने के तरीके की वजह से Elvis operator कहते हैं — अगर $value truthy है तो इसे खुद return करता है, नहीं तो $fallback। यह $value को दो बार दोहराए बिना $value ? $value : $fallback का shorthand है।
उदाहरण: Short Ternary Operator
<?php
// Declare `$value`, set to ""
$value = "";
// Declare `$result`, set to `$value ?: "fallback"`
$result = $value ?: "fallback";
// Print `$result` to the output
echo $result;
?>
Login to try C/C++/Java/PHP code in the editor
Null Coalescing Operator
$value ?? $fallback, null coalescing operator, $value return करता है जब तक यह null या पूरी तरह undefined न हो, ऐसे case में यह $fallback return करता है।
ऊपर वाले short ternary के उलट, यह specifically null/undefined check करता है किसी भी falsy value के बजाय, इसलिए 0 या '' fallback trigger करने के बजाय untouched pass हो जाते हैं।
उदाहरण: Null Coalescing Operator
<?php
// Declare `$value`, set to `0`
$value = 0;
// Print `$value ?? "fallback"` to the output
echo $value ?? "fallback";
?>
Login to try C/C++/Java/PHP code in the editor
Ternary के साथ Direct Output
क्योंकि ternary operator खुद एक value वाला expression है, आप इसे सीधे किसी echo call के अंदर डाल सकते हैं — echo $loggedIn ? 'Welcome back' : 'Please log in'; — बिना पहले chosen string रखने के लिए एक अलग variable की ज़रूरत के।
उदाहरण: Direct Output with Ternary
<?php
// Declare `$loggedIn`, set to `true`
$loggedIn = true;
// Print `$loggedIn ? 'Welcome back' : 'Please log in'` to the output
echo $loggedIn ? 'Welcome back' : 'Please log in';
?>
Login to try C/C++/Java/PHP code in the editor
Nested Ternary Operators
एक ternary को दूसरे के अंदर nest करना आपको choices की एक छोटी chain एक line में express करने देता है, लेकिन दो levels के आगे readability तेज़ी से गिरती है।
उस point के आगे, एक match expression या एक plain if/elseif chain लगभग हमेशा same logic को ज़्यादा clearly communicate करती है।
उदाहरण: Nested Ternary Operators
<?php
// Declare `$score`, set to `75`
$score = 75;
// Declare `$grade`, set to `$score >= 90 ? "A" : ($score >= 70 ? "B" : "C")`
$grade = $score >= 90 ? "A" : ($score >= 70 ? "B" : "C");
// Print `$grade` to the output
echo $grade;
?>
Login to try C/C++/Java/PHP code in the editor
- parentheses के बिना ternaries nest करना, जैसे
$a ? x : $b ? y : z, जो PHP 8 में एक fatal error है। ?:इस्तेमाल करना जब0या""एक valid value हो, क्योंकि short ternary उन्हें falsy मानता है और fallback ले लेता है।- एक empty string पकड़ने के लिए
??इस्तेमाल करना, जबकि यह सिर्फnullया एक undefined variable के लिए fall back करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: