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

PHP If Operators

एक if statement उतना ही उपयोगी है जितनी उसके अंदर की condition -- और PHP आपको conditions बनाने के लिए comparison और logical operators का एक पूरा set देता है, एक simple equality check से लेकर AND/OR logic से जुड़े कई rules के एक complex combination तक। इन operators को अच्छे से जानना ही एक plain if को एक precise decision-making tool बना देता है।
Syntax
php
if ($a == $b && $c > $d) {
    // comparison operators combined with && || !
}

Conditions में Comparison Operators

==, !=, <, >, <=, और >= जैसे comparison operators हर एक दो values compare करके एक true या false result produce करते हैं, और वह result exactly वही है जो एक if statement को यह decide करने के लिए चाहिए कि कौन सी branch चलानी है।

उदाहरण: Comparison Operators in Conditions

php
<?php
// Declare `$score`, set to `85`
$score = 85;
// Check whether `$score >= 60`
if ($score >= 60) {
    // Print "Pass" to the output
    echo "Pass";
}
?>

Strict बनाम Loose Equality

== (loose equality) compare करने से पहले दोनों sides को एक common type में convert कर देता है, जो पुराने PHP versions में "abc" == 0 के true होने जैसे surprising results produce कर सकता है। === (strict equality) इसके बजाय value AND type दोनों को exactly match करने की माँग करता है, उस तरह की type-coercion surprise से पूरी तरह बचते हुए।

उदाहरण: Strict vs Loose Equality

php
<?php
// Print a detailed dump (with types) of `"abc" == 0`
var_dump("abc" == 0);
// Print a detailed dump (with types) of `"abc" === 0`
var_dump("abc" === 0);
?>

Logical Operators से Conditions Combine करना

&& (AND) को दोनों sides true होने चाहिए, || (OR) को कम से कम एक side true होना चाहिए, और ! (NOT) एक single condition को invert करता है -- ये आपको एक साथ कई rules check करने वाला एक single if statement बनाने देते हैं, कई अलग if statements nest करने के बजाय।

उदाहरण: Combining Conditions with Logical Operators

php
<?php
// Declare `$age`, set to `25`
$age = 25;
// Declare `$hasLicense`, set to `true`
$hasLicense = true;
// Check whether `$age >= 18 && $hasLicense`
if ($age >= 18 && $hasLicense) {
    // Print "Can drive" to the output
    echo "Can drive";
}
?>

! से एक Condition Negate करना

! operator एक boolean value flip करता है: ! true false बन जाता है, और ! false true बन जाता है। किसी condition को ! में wrap करना किसी चीज़ का "opposite" check करने के लिए उपयोगी है, जैसे यह कि क्या कोई variable empty NOT है या कोई value target से NOT match करती है।

उदाहरण: Negating a Condition with !

php
<?php
// Declare `$value`, set to ""
$value = "";
// Check whether `!$value`
if (!$value) {
    // Print "Value is empty" to the output
    echo "Value is empty";
}
?>

Word Operators: and, or, not

PHP word-form logical operators भी देता है -- and, or, not -- जो लगभग &&, ||, और ! जैसे behave करते हैं, लेकिन उनकी operator precedence कहीं कम है।

यह कम precedence assignment operator = के साथ mix होने पर surprising bugs का कारण बन सकती है, इसलिए ज़्यादातर style guides ordinary conditions में symbolic forms (&&, ||, !) पर टिके रहने की recommendation देती हैं।

उदाहरण: Word Operators: and, or, not

php
<?php
// Declare `$loggedIn`, set to `true and false`
$loggedIn = true and false;
// Print a detailed dump (with types) of `$loggedIn`
var_dump($loggedIn);
?>
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. == (loose equality) इस्तेमाल करना जब असल में === (strict equality) चाहिए, PHP की type juggling को surprising true results produce करने देते हुए, जैसे "0" == false।
  2. && (logical AND) को & (bitwise AND) से confuse करना -- वे similar दिखते हैं लेकिन पूरी तरह अलग चीज़ें करते हैं, और किसी if के अंदर गलत वाला इस्तेमाल करना चुपचाप गलत results produce करता है।
  3. compound conditions को clearly group करने के लिए parentheses के बिना बहुत लंबा लिखना, actual logic को एक नज़र में verify करना मुश्किल बनाते हुए।
चैप्टर सारांश
  • Comparison operators (==, ===, !=, !==, <, >, <=, >=) किसी if condition के अंदर इस्तेमाल होने वाले individual true/false checks बनाते हैं।
  • Logical operators (&&, ||, !, और उनके word forms and/or/not) कई comparisons को एक overall condition में combine करते हैं।
  • === और !== value और type दोनों check करते हैं, == और != के produce कर सकने वाले surprising type-coercion results से बचते हुए।

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.