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

PHP Nested If

कुछ decisions एक से ज़्यादा independent question पर depend करते हैं -- पहले check करें कि कोई user logged in है, और तभी check करें कि उसके पास admin rights हैं या नहीं। एक nested if बस एक if statement है जो किसी दूसरे if statement की body के अंदर रखा गया है, आपको दूसरी condition सिर्फ तभी check करने देते हुए जब पहली पहले से satisfy हो चुकी हो।
Syntax
php
if (outer_condition) {
    if (inner_condition) {
        // runs when both are true
    }
}

Basic Nested If Structure

एक if statement को दूसरे की body के अंदर रखना एक nested if बनाता है -- outer condition एक gate की तरह act करती है, और inner condition सिर्फ तभी check होती है जब execution पहले से outer block में entered हो चुका हो।

उदाहरण: Basic Nested If Structure

php
<?php
// Declare `$loggedIn`, set to `true`
$loggedIn = true;
// Check whether `$loggedIn`
if ($loggedIn) {
    // Declare `$isAdmin`, set to `true`
    $isAdmin = true;
    // Check whether `$isAdmin`
    if ($isAdmin) {
        // Print "Welcome, admin!" to the output
        echo "Welcome, admin!";
    }
}
?>

एक Else Branch के अंदर Nesting

एक nested if एक else block के अंदर भी उतनी ही आसानी से रह सकता है जितना एक if block के अंदर, आपको outer condition की कौन सी branch ली गई इस पर depend करते हुए sub-cases का पूरी तरह अलग set handle करने देते हुए।

उदाहरण: Nesting Inside an Else Branch

php
<?php
// Declare `$loggedIn`, set to `false`
$loggedIn = false;
// Check whether `$loggedIn`
if ($loggedIn) {
    // Print "Welcome back!" to the output
    echo "Welcome back!";
// Otherwise, run this branch
} else {
    // Declare `$hasAccount`, set to `false`
    $hasAccount = false;
    // Check whether `$hasAccount`
    if ($hasAccount) {
        // Print "Please log in." to the output
        echo "Please log in.";
    // Otherwise, run this branch
    } else {
        // Print "Please create an account." to the output
        echo "Please create an account.";
    }
}
?>

&& से Conditions कब Combine करें

अगर एक inner if को सिर्फ तभी चलना है जब outer और inner दोनों conditions true हों -- और outcomes का कोई और combination मायने नहीं रखता -- उन्हें && के साथ एक single if में combine करना कम nesting और कम indentation के साथ exactly same behavior produce करता है।

उदाहरण: When to Combine Conditions with && Instead

php
<?php
// Declare `$age`, set to `20`
$age = 20;
// Declare `$hasTicket`, set to `true`
$hasTicket = true;
// Check whether `$age >= 18 && $hasTicket`
if ($age >= 18 && $hasTicket) {
    // Print "Entry allowed" to the output
    echo "Entry allowed";
}
?>

Multi-Step Validation के लिए Nested If

Nested ifs multi-step validation के लिए एक natural fit हैं, जहाँ हर step सिर्फ तभी check होना चाहिए जब पिछला step पहले से pass हो चुका हो -- जैसे किसी form field का length check करने से पहले उसका exist करना confirm करना, क्योंकि एक missing field की length check करना एक error का कारण बनेगा।

उदाहरण: Nested If for Multi-Step Validation

php
<?php
// Declare `$field`, set to "hello"
$field = "hello";
// Check whether `isset($field)`
if (isset($field)) {
    // Check whether `strlen($field) > 3`
    if (strlen($field) > 3) {
        // Print "Field is valid." to the output
        echo "Field is valid.";
    }
}
?>

Readability: Deep Nesting के Alternatives

दो या तीन levels के आगे, nested ifs को follow करना genuinely मुश्किल हो जाता है -- early returns (किसी condition के fail होते ही किसी function से exit करना), && से conditions combine करना, या एक match/switch expression पर switch करने जैसी techniques deeply nested logic को कहीं ज़्यादा आसानी से scan होने लायक चीज़ में flatten कर सकती हैं।

उदाहरण: Readability: Alternatives to Deep Nesting

php
<?php
// Define the function `checkAccess` taking `$age`, `$hasTicket`
function checkAccess($age, $hasTicket) {
    // Check whether `!$hasTicket`
    if (!$hasTicket) {
        // Return `"No ticket"` from this function
        return "No ticket";
    }
    // Check whether `$age < 18`
    if ($age < 18) {
        // Return `"Too young"` from this function
        return "Too young";
    }
    // Return `"Access granted"` from this function
    return "Access granted";
}
// Print `checkAccess(20, true)` to the output
echo checkAccess(20, true);
?>
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. if statements को कई levels गहरे nest करना, indentation का एक hard-to-follow staircase produce करते हुए बजाय जहाँ यह simpler होता वहाँ && से conditions combine करने के।
  2. यह भूल जाना कि एक inner if सिर्फ तभी चलता है जब outer if की condition पहले true रही हो -- यह debug करते समय confusion का एक common source है कि कोई block "कभी क्यों नहीं चलता"।
  3. deeply nested blocks में braces को misalign करना, यह बताना मुश्किल बनाते हुए कि कौन सी closing brace किस if statement से belong करती है।
चैप्टर सारांश
  • एक nested if एक if statement है जो पूरी तरह किसी दूसरे if (या else) block की body के अंदर लिखा गया है।
  • Inner condition सिर्फ तभी evaluate होती है जब outer condition पहले true रही हो।
  • दो या ज़्यादा independent conditions जिनका सबका true होना ज़रूरी है उन्हें अक्सर nested ifs से && वाले एक single if में simplify किया जा सकता है।

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.