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

PHP if Statement

एक if statement एक rule जैसा है जो कहता है 'अगर बारिश हो रही है, umbrella ले लो'। अंदर का code सिर्फ तभी चलता है जब condition true हो, नहीं तो PHP बस इसे skip कर देता है।
Syntax
php
if (condition) {
    // runs only when condition is true
}

Basic if Statement

एक if statement अपना block सिर्फ तभी चलाता है जब parentheses के अंदर की condition true evaluate हो; अगर यह false है, PHP पूरे block को skip कर देता है और सीधे इसके बाद जो भी है वहाँ चला जाता है — कोई error नहीं, कोई output नहीं, बस चुपचाप execution continue।

उदाहरण: Basic if Statement

php
<?php
// Declare `$age`, set to `20`
$age = 20;
// Check whether `$age >= 18`
if ($age >= 18) {
    // Print "You are an adult." to the output
    echo "You are an adult.";
}
?>

Conditions के अंदर Comparison

=== बिना किसी automatic conversion के value और type दोनों compare करता है, जो इसे किसी if condition के अंदर safer default बनाता है — if ($status === active) गलती से 1 या "1" से match नहीं करेगा जैसे looser == operator कभी-कभी कर सकता है।

उदाहरण: Comparison inside Conditions

php
<?php
// Declare `$status`, set to "active"
$status = "active";
// Check whether `$status === 'active'`
if ($status === 'active') {
    // Print "Status is active." to the output
    echo "Status is active.";
}
?>

Conditions में Logical AND (&&)

किसी condition के अंदर && को block चलने से पहले हर जुड़े हुए expression का true होना चाहिए — if ($age >= 18 && $hasConsent) सिर्फ तभी execute होता है जब दोनों checks pass हों, जो एक साथ कई independent requirements enforce करने के लिए exactly सही pattern है।

उदाहरण: Logical AND (&&) in Conditions

php
<?php
// Declare `$age`, set to `20`
$age = 20;
// Declare `$hasConsent`, set to `true`
$hasConsent = true;
// Check whether `$age >= 18 && $hasConsent`
if ($age >= 18 && $hasConsent) {
    // Print "Access granted." to the output
    echo "Access granted.";
}
?>

Conditions में Logical OR (||)

किसी condition के अंदर || block को चलने देता है अगर जुड़े हुए expressions में से *कोई एक* भी true हो, जो same outcome तक पहुँचने के कई acceptable paths वाली situations में suit करता है — जैसे एक username या email address दोनों से login accept करना।

उदाहरण: Logical OR (||) in Conditions

php
<?php
// Declare `$username`, set to ""
$username = "";
// Declare `$email`, set to "[email protected]"
$email = "[email protected]";
// Check whether `$username || $email`
if ($username || $email) {
    // Print "Login accepted." to the output
    echo "Login accepted.";
}
?>

Nested if Statements

एक if को दूसरे के अंदर रखना आपको एक दूसरी condition सिर्फ पहले वाली pass होने के बाद check करने देता है — यह तब उपयोगी है जब दूसरा check genuinely सिर्फ उस context में मायने रखता हो (मान लीजिए, किसी file के contents check करना सिर्फ यह confirm करने के बाद कि file exist ही करती है)।

उदाहरण: Nested if Statements

php
<?php
// Declare `$fileExists`, set to `true`
$fileExists = true;
// Check whether `$fileExists`
if ($fileExists) {
    // Declare `$content`, set to "sample data"
    $content = "sample data";
    // Check whether `!empty($content)`
    if (!empty($content)) {
        // Print "File exists and has content." to the output
        echo "File exists and has content.";
    }
}
?>
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. if ($x == 5) के बजाय if ($x = 5) (assignment) लिखना, जो हमेशा block चलाता है।
  2. condition के बाद एक semicolon लगाना, if ($x > 5);, ताकि following block हमेशा execute हो।
  3. यह उम्मीद करना कि if ($str) "0" के लिए true होगा, जबकि PHP में "0" falsy है।

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.