PHP if-else Statement
In this page:
if (condition) {
// runs when condition is true
} else {
// runs otherwise
}
Standard If-Else
if/else guarantee करता है कि दो blocks में से exactly एक चलेगा: if block जब condition true हो, else block जब यह false हो।
कोई scenario नहीं जहाँ कोई भी न चले, और कोई नहीं जहाँ दोनों चलें — दोनों branches construction से mutually exclusive हैं।
उदाहरण: Standard If-Else
<?php
// Declare `$age`, set to `15`
$age = 15;
// Check whether `$age >= 18`
if ($age >= 18) {
// Print "Adult" to the output
echo "Adult";
// Otherwise, run this branch
} else {
// Print "Minor" to the output
echo "Minor";
}
?>
Login to try C/C++/Java/PHP code in the editor
Conditional Assignment
किसी condition के आधार पर एक variable की value set करने के लिए सिर्फ if/else इस्तेमाल करना common है, लेकिन सबसे simple cases के लिए — दो values में से एक assign करना — ternary operator ($x = $cond ? $a : $b;) identical logic को एक single line में express करता है।
उदाहरण: Conditional Assignment
<?php
// Declare `$cond`, set to `true`
$cond = true;
// Declare `$x`, set to `$cond ? "yes" : "no"`
$x = $cond ? "yes" : "no";
// Print `$x` to the output
echo $x;
?>
Login to try C/C++/Java/PHP code in the editor
Boolean Toggles
Boolean variables naturally if/else के साथ pair होकर एक condition को plain English जैसा पढ़ने लायक बनाते हैं: if ($isPremium) { ... } else { ... } यह तुरंत clear करता है कि किस पर branch किया जा रहा है, एक ज़्यादा complex expression में दबी हुई condition की तुलना में।
उदाहरण: Boolean Toggles
<?php
// Declare `$isPremium`, set to `false`
$isPremium = false;
// Check whether `$isPremium`
if ($isPremium) {
// Print "Premium features unlocked" to the output
echo "Premium features unlocked";
// Otherwise, run this branch
} else {
// Print "Upgrade to unlock features" to the output
echo "Upgrade to unlock features";
}
?>
Login to try C/C++/Java/PHP code in the editor
Alternative Syntax
PHP का alternative syntax curly braces को condition के बाद एक colon और block बंद करने के लिए एक endif; से replace करता है।
यह plain PHP files में शायद ही कभी इस्तेमाल होता है, लेकिन जब यह सीधे किसी HTML template के अंदर interleaved हो तो यह brace-heavy code से कहीं ज़्यादा cleanly पढ़ता है।
उदाहरण: Alternative Syntax
<?php $isAdmin = true; ?>
<?php // Branch which HTML block gets output, based on $isAdmin ?>
<?php if ($isAdmin): ?>
<p>Admin panel</p>
<?php else: ?>
<p>Access denied</p>
<?php endif; ?>
Login to try C/C++/Java/PHP code in the editor
Array Checks
अपनी if condition के अंदर array_key_exists() या isset() जैसा function call करना — array से एक value पढ़ने की कोशिश करने से पहले — उस warning से बचाता है जो PHP तब throw करता है जब आप किसी ऐसी key access करें जो असल में वहाँ है ही नहीं।
उदाहरण: Array Checks
<?php
// Declare `$user` as an array: `["name" => "Alice"]`
$user = ["name" => "Alice"];
// Check whether `isset($user['email'])`
if (isset($user['email'])) {
// Print `$user['email']` to the output
echo $user['email'];
// Otherwise, run this branch
} else {
// Print "No email set." to the output
echo "No email set.";
}
?>
Login to try C/C++/Java/PHP code in the editor
elseको गलत brace पर रखना, या colon syntax के साथelse ifलिखना, जो एक parse error का कारण बनता है (वहाँelseifइस्तेमाल करें)।- एक boolean return करने के लिए
if/elseइस्तेमाल करना, जैसेif ($x > 5) { return true; } else { return false; }, जबकिreturn $x > 5;काफी है। - multi-statement
elsebodies के आसपास braces भूल जाना, ताकि सिर्फ पहली lineelseसे belong करे।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: