PHP Logical Operators
In this page:
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
Logical NOT (!)
! खुद एक single boolean को invert करता है — true false बन जाता है और उल्टा भी। यह अक्सर किसी function call या condition के आगे इसका meaning सीधे flip करने के लिए दिखता है, जैसे if (!isset($value)), जिसे 'if value is not set' पढ़ा जाता है।
उदाहरण: Logical NOT (!)
<?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";
}
?>
Login to try C/C++/Java/PHP code in the editor
Logical XOR (xor)
xor सिर्फ तभी true return करता है जब exactly एक side true हो — दोनों true या दोनों false, दोनों false produce करते हैं। यह everyday code में &&/|| से कम common है, लेकिन यह exactly सही tool है जब कोई rule genuinely 'एक या दूसरा, लेकिन दोनों नहीं' मतलब रखता हो।
उदाहरण: Logical XOR (xor)
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
and/orको=के साथ mix करना, जैसे$ok = true and false;, जहाँ$oktrueबन जाता है क्योंकिandकी precedence assignment से कम होती है।- यह उम्मीद करना कि
&&के दोनों sides हमेशा चलेंगे, जबकि short-circuiting अगर left false है तो right skip कर देता है, इसलिए वहाँ मौजूद कोई function call शायद execute न हो। ||को??से confuse करना, ताकि$x || defaultfallback value के बजाय एक booleantruereturn करे।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: