PHP elseif Ladder
In this page:
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
कई elseif Blocks
आप logic को जितने चाहें उतने elseif blocks chain कर सकते हैं, जो इसे तब natural structure बनाता है जब किसी value को सिर्फ एक simple true/false split के बजाय कई distinct categories या ranges में से किसी एक में sort करना हो।
उदाहरण: Multiple elseif Blocks
<?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";
}
?>
Login to try C/C++/Java/PHP code in the editor
Conditions को Order करना
क्योंकि PHP पहली true condition पर रुक जाता है, order मायने रखता है: ladder के top के पास एक broad, आसानी से satisfy होने वाली condition रखना चुपचाप उन cases को 'चुरा' सकता है जो नीचे किसी ज़्यादा specific condition तक पहुँचने वाले थे — हमेशा most specific से most general की ओर order करें।
उदाहरण: Ordering Conditions
<?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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
Default Fallback Else
बिना किसी condition के attached एक final else उन हर cases को पकड़ता है जिन्हें पहले की किसी भी branch ने match नहीं किया। यह सिर्फ तभी चलता है जब इसके ऊपर पूरी ladder fall through हो जाए, इसे उन values के लिए safety net बनाते हुए जिनकी आपने explicitly योजना नहीं बनाई थी।
उदाहरण: Default Fallback Else
<?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";
}
?>
Login to try C/C++/Java/PHP code in the editor
- colon syntax में
elseifके बजायelse ifलिखना, जो एक parse error का कारण बनता है। $score >= 90से पहले$score >= 50जैसी एक broad condition रखना, ताकि बाद वाली, ज़्यादा specific branch कभी पहुँची ही न जाए।elseifके बजाय अलगifstatements इस्तेमाल करना, ताकि कई branches चल सकें जब सिर्फ एक को चलना चाहिए था।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: