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

PHP if-else Statement

if-else exactly दो paths वाला एक fork in the road है: यह करो अगर question true है, या वह करो अगर नहीं है। दोनों में से एक हमेशा होता है।
Syntax
php
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
<?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";
}
?>

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
<?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;
?>

Boolean Toggles

Boolean variables naturally if/else के साथ pair होकर एक condition को plain English जैसा पढ़ने लायक बनाते हैं: if ($isPremium) { ... } else { ... } यह तुरंत clear करता है कि किस पर branch किया जा रहा है, एक ज़्यादा complex expression में दबी हुई condition की तुलना में।

उदाहरण: Boolean Toggles

php
<?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";
}
?>

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
<?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; ?>

Array Checks

अपनी if condition के अंदर array_key_exists() या isset() जैसा function call करना — array से एक value पढ़ने की कोशिश करने से पहले — उस warning से बचाता है जो PHP तब throw करता है जब आप किसी ऐसी key access करें जो असल में वहाँ है ही नहीं।

उदाहरण: Array Checks

php
<?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.";
}
?>
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. else को गलत brace पर रखना, या colon syntax के साथ else if लिखना, जो एक parse error का कारण बनता है (वहाँ elseif इस्तेमाल करें)।
  2. एक boolean return करने के लिए if/else इस्तेमाल करना, जैसे if ($x > 5) { return true; } else { return false; }, जबकि return $x > 5; काफी है।
  3. multi-statement else bodies के आसपास braces भूल जाना, ताकि सिर्फ पहली line else से belong करे।

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.