← Back to PHP Course | Chapter 3: Operators | Lesson 3 of 8

PHP Logical Operators

Logical operators yes-or-no questions को जोड़ते हैं, जैसे 'क्या मेरे पास मेरा coat है AND क्या बारिश हो रही है?'। ये PHP को कई conditions से एक साथ decisions लेने में मदद करते हैं।
Syntax
php
if (condition1 && condition2) { }   // both true
if (condition1 || condition2) { }   // at least one true
if (!condition) { }                 // not

Logical AND (&&, and)

&& (या word and) सिर्फ तभी true evaluate होता है जब दोनों sides true हों, और PHP इसे short-circuit करता है: अगर left side पहले से false है, right side कभी चलता ही नहीं।

यह short-circuiting ही वजह है कि && आमतौर पर errors से बचाव के लिए इस्तेमाल होता है, जैसे isset($x) && $x > 0।

उदाहरण: Logical AND (&&, and)

php
<?php
// Declare `$x`, set to `5`
$x = 5;
// Check whether `isset($x) && $x > 0`
if (isset($x) && $x > 0) {
    // Print "x is set and positive" to the output
    echo "x is set and positive";
}
?>

Logical OR (||, or)

|| (या or) *किसी भी एक* side के true होते ही true evaluate होता है, और यह opposite तरीके से short-circuit करता है — अगर left side पहले से true है, PHP right side को evaluate करने की तकलीफ़ ही नहीं उठाता, जो मायने रख सकता है अगर उस right side के side effects हों।

उदाहरण: Logical OR (||, or)

php
<?php
// Declare `$loggedIn`, set to `false`
$loggedIn = false;
// Declare `$isAdmin`, set to `true`
$isAdmin = true;
// Check whether `$loggedIn || $isAdmin`
if ($loggedIn || $isAdmin) {
    // Print "Access granted" to the output
    echo "Access granted";
}
?>

Logical NOT (!)

! खुद एक single boolean को invert करता है — true false बन जाता है और उल्टा भी। यह अक्सर किसी function call या condition के आगे इसका meaning सीधे flip करने के लिए दिखता है, जैसे if (!isset($value)), जिसे 'if value is not set' पढ़ा जाता है।

उदाहरण: Logical NOT (!)

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

Logical XOR (xor)

xor सिर्फ तभी true return करता है जब exactly एक side true हो — दोनों true या दोनों false, दोनों false produce करते हैं। यह everyday code में &&/|| से कम common है, लेकिन यह exactly सही tool है जब कोई rule genuinely 'एक या दूसरा, लेकिन दोनों नहीं' मतलब रखता हो।

उदाहरण: Logical XOR (xor)

php
<?php
// Print a detailed dump (with types) of `true xor false`
var_dump(true xor false);
// Print a detailed dump (with types) of `true xor true`
var_dump(true xor true);
?>

Logical Operators को Combine करना

PHP logical operators को precedence rules के हिसाब से left से right evaluate करता है, और वे rules हमेशा वह नहीं होते जो आप guess करेंगे — and/or &&/|| से ज़्यादा loosely bind करते हैं।

Sub-expressions को parentheses में wrap करना कोई भी ambiguity हटा देता है और intended grouping को पढ़ने वाले के लिए भी obvious बना देता है।

उदाहरण: Combining Logical Operators

php
<?php
// Declare `$a`, set to `true`
$a = true;
// Declare `$b`, set to `false`
$b = false;
// Declare `$c`, set to `true`
$c = true;
// Print `($a && $b) || $c ? "true" : "false"` to the output
echo ($a && $b) || $c ? "true" : "false";
?>
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. and/or को = के साथ mix करना, जैसे $ok = true and false;, जहाँ $ok true बन जाता है क्योंकि and की precedence assignment से कम होती है।
  2. यह उम्मीद करना कि && के दोनों sides हमेशा चलेंगे, जबकि short-circuiting अगर left false है तो right skip कर देता है, इसलिए वहाँ मौजूद कोई function call शायद execute न हो।
  3. || को ?? से confuse करना, ताकि $x || default fallback value के बजाय एक boolean true return करे।

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.