← Back to PHP Course | Chapter 3: Operators | Lesson 7 of 8

PHP Ternary Operator

Ternary operator एक one-line 'this or that' choice है, जैसे पूछना 'क्या धूप है? तो sunglasses पहनो, नहीं तो umbrella ले लो'। यह एक छोटे if-else को एक single short expression में निचोड़ देता है।
Syntax
php
$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
<?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;
?>

Short Ternary Operator

$value ?: $fallback — short ternary, जिसे कभी-कभी इसके दिखने के तरीके की वजह से Elvis operator कहते हैं — अगर $value truthy है तो इसे खुद return करता है, नहीं तो $fallback। यह $value को दो बार दोहराए बिना $value ? $value : $fallback का shorthand है।

उदाहरण: Short Ternary Operator

php
<?php
// Declare `$value`, set to ""
$value = "";
// Declare `$result`, set to `$value ?: "fallback"`
$result = $value ?: "fallback";
// Print `$result` to the output
echo $result;
?>

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
<?php
// Declare `$value`, set to `0`
$value = 0;
// Print `$value ?? "fallback"` to the output
echo $value ?? "fallback";
?>

Ternary के साथ Direct Output

क्योंकि ternary operator खुद एक value वाला expression है, आप इसे सीधे किसी echo call के अंदर डाल सकते हैं — echo $loggedIn ? 'Welcome back' : 'Please log in'; — बिना पहले chosen string रखने के लिए एक अलग variable की ज़रूरत के।

उदाहरण: Direct Output with Ternary

php
<?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';
?>

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
<?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;
?>
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. parentheses के बिना ternaries nest करना, जैसे $a ? x : $b ? y : z, जो PHP 8 में एक fatal error है।
  2. ?: इस्तेमाल करना जब 0 या "" एक valid value हो, क्योंकि short ternary उन्हें falsy मानता है और fallback ले लेता है।
  3. एक empty string पकड़ने के लिए ?? इस्तेमाल करना, जबकि यह सिर्फ null या एक undefined variable के लिए fall back करता है।

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.