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

PHP elseif Ladder

एक elseif ladder एक के बाद एक पूछे गए questions की row जैसा है: क्या आप 5 से कम हैं? 12 से कम? 18 से कम? PHP पहले yes पर रुक जाता है और बाकी सबको ignore कर देता है।
Syntax
php
if (condition1) {
    // ...
} elseif (condition2) {
    // ...
} else {
    // none matched
}

Basic elseif Structure

एक elseif ladder top से bottom तक conditions की एक series test करता है और उस *पहले* condition से belong करने वाला block चलाता है जो true evaluate होती है, फिर इसके बाद की हर condition और block skip कर देता है — भले ही एक बाद वाली condition भी match करती होती।

उदाहरण: Basic elseif Structure

php
<?php
// Declare `$score`, set to `85`
$score = 85;
// Check whether `$score >= 90`
if ($score >= 90) {
    // Print "A" to the output
    echo "A";
// Otherwise, if `$score >= 80`, run this branch
} elseif ($score >= 80) {
    // Print "B" to the output
    echo "B";
// Otherwise, run this branch
} else {
    // Print "C" to the output
    echo "C";
}
?>

कई elseif Blocks

आप logic को जितने चाहें उतने elseif blocks chain कर सकते हैं, जो इसे तब natural structure बनाता है जब किसी value को सिर्फ एक simple true/false split के बजाय कई distinct categories या ranges में से किसी एक में sort करना हो।

उदाहरण: Multiple elseif Blocks

php
<?php
// Declare `$grade`, set to `72`
$grade = 72;
// Check whether `$grade >= 90`
if ($grade >= 90) {
    // Print "A" to the output
    echo "A";
// Otherwise, if `$grade >= 80`, run this branch
} elseif ($grade >= 80) {
    // Print "B" to the output
    echo "B";
// Otherwise, if `$grade >= 70`, run this branch
} elseif ($grade >= 70) {
    // Print "C" to the output
    echo "C";
// Otherwise, if `$grade >= 60`, run this branch
} elseif ($grade >= 60) {
    // Print "D" to the output
    echo "D";
// Otherwise, run this branch
} else {
    echo "F";
}
?>

Conditions को Order करना

क्योंकि PHP पहली true condition पर रुक जाता है, order मायने रखता है: ladder के top के पास एक broad, आसानी से satisfy होने वाली condition रखना चुपचाप उन cases को 'चुरा' सकता है जो नीचे किसी ज़्यादा specific condition तक पहुँचने वाले थे — हमेशा most specific से most general की ओर order करें।

उदाहरण: Ordering Conditions

php
<?php
// Declare `$age`, set to `25`
$age = 25;
// Check whether `$age >= 0`
if ($age >= 0) {
    // Print "Matched too broad first condition" to the output
    echo "Matched too broad first condition";
// Otherwise, if `$age >= 18`, run this branch
} elseif ($age >= 18) {
    // Print "This never runs -- unreachable" to the output
    echo "This never runs -- unreachable";
}
?>

Alternative Colon Syntax

Alternative colon syntax यहाँ plain if/else जैसा ही काम करता है, लेकिन इस style का इस्तेमाल करते समय PHP को elseif एक word (न कि else if) की तरह लिखना ज़रूरी है — एक छोटा लेकिन आसानी से miss होने वाला syntax rule जो specifically colon form के लिए है।

उदाहरण: Alternative Colon Syntax

php
<?php
// Declare `$role`, set to "editor"
$role = "editor";
// Check whether `$role === "admin"`
if ($role === "admin"):
    // Print "Admin access" to the output
    echo "Admin access";
// Otherwise, if `$role === "editor"`, run this branch
elseif ($role === "editor"):
    // Print "Editor access" to the output
    echo "Editor access";
// Otherwise, run this branch
else:
    // Print "No access" to the output
    echo "No access";
endif;
?>

Default Fallback Else

बिना किसी condition के attached एक final else उन हर cases को पकड़ता है जिन्हें पहले की किसी भी branch ने match नहीं किया। यह सिर्फ तभी चलता है जब इसके ऊपर पूरी ladder fall through हो जाए, इसे उन values के लिए safety net बनाते हुए जिनकी आपने explicitly योजना नहीं बनाई थी।

उदाहरण: Default Fallback Else

php
<?php
// Declare `$day`, set to "Sunday"
$day = "Sunday";
// Check whether `$day === "Saturday"`
if ($day === "Saturday") {
    // Print "Weekend" to the output
    echo "Weekend";
// Otherwise, if `$day === "Monday"`, run this branch
} elseif ($day === "Monday") {
    // Print "Start of week" to the output
    echo "Start of week";
// Otherwise, run this branch
} else {
    // Print "Some other day" to the output
    echo "Some other day";
}
?>
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. colon syntax में elseif के बजाय else if लिखना, जो एक parse error का कारण बनता है।
  2. $score >= 90 से पहले $score >= 50 जैसी एक broad condition रखना, ताकि बाद वाली, ज़्यादा specific branch कभी पहुँची ही न जाए।
  3. elseif के बजाय अलग if statements इस्तेमाल करना, ताकि कई branches चल सकें जब सिर्फ एक को चलना चाहिए था।

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.