PHP If Operators
In this page:
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
// Declare `$score`, set to `85`
$score = 85;
// Check whether `$score >= 60`
if ($score >= 60) {
// Print "Pass" to the output
echo "Pass";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
! से एक 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
// Declare `$value`, set to ""
$value = "";
// Check whether `!$value`
if (!$value) {
// Print "Value is empty" to the output
echo "Value is empty";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// Declare `$loggedIn`, set to `true and false`
$loggedIn = true and false;
// Print a detailed dump (with types) of `$loggedIn`
var_dump($loggedIn);
?>
Login to try C/C++/Java/PHP code in the editor
- == (loose equality) इस्तेमाल करना जब असल में === (strict equality) चाहिए, PHP की type juggling को surprising true results produce करने देते हुए, जैसे "0" == false।
- && (logical AND) को & (bitwise AND) से confuse करना -- वे similar दिखते हैं लेकिन पूरी तरह अलग चीज़ें करते हैं, और किसी if के अंदर गलत वाला इस्तेमाल करना चुपचाप गलत results produce करता है।
- 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 से बचते हुए।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: