PHP Nested If
In this page:
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
// 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!";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
एक Else Branch के अंदर Nesting
एक nested if एक else block के अंदर भी उतनी ही आसानी से रह सकता है जितना एक if block के अंदर, आपको outer condition की कौन सी branch ली गई इस पर depend करते हुए sub-cases का पूरी तरह अलग set handle करने देते हुए।
उदाहरण: Nesting Inside an Else Branch
<?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.";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
&& से 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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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.";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
- if statements को कई levels गहरे nest करना, indentation का एक hard-to-follow staircase produce करते हुए बजाय जहाँ यह simpler होता वहाँ && से conditions combine करने के।
- यह भूल जाना कि एक inner if सिर्फ तभी चलता है जब outer if की condition पहले true रही हो -- यह debug करते समय confusion का एक common source है कि कोई block "कभी क्यों नहीं चलता"।
- 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 किया जा सकता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: