PHP Relational Operators
In this page:
$a == $b; // equal (after type conversion)
$a === $b; // equal and same type
$a != $b; // not equal
$a !== $b; // not identical
$a > $b; $a < $b; $a >= $b; $a <= $b;
Equality और Inequality
== check करता है कि दो values equal हैं या नहीं *बाद में जब* PHP ज़रूरत पड़ने पर उन्हें एक common type में convert कर दे, इसलिए "5" == 5 true evaluate होता है भले ही एक side एक string हो।
!= बस इसका opposite है, वही type coercion apply होने के बाद values को अलग flag करता है।
उदाहरण: Equality and Inequality
<?php
// Print a detailed dump (with types) of `"5" == 5`
var_dump("5" == 5);
// Print a detailed dump (with types) of `5 != 6`
var_dump(5 != 6);
?>
Login to try C/C++/Java/PHP code in the editor
Strict Equality और Inequality
=== को value *और* type दोनों exactly match चाहिए, इसलिए "5" === 5 false है — एक string कभी strictly किसी integer के बराबर नहीं हो सकती, चाहे यह जैसा भी दिखे। !== इसका inverse है।
Default रूप से strict versions इस्तेमाल करना subtle type-coercion bugs की एक पूरी category से बचाता है।
उदाहरण: Strict Equality and Inequality
<?php
// Print a detailed dump (with types) of `"5" === 5`
var_dump("5" === 5);
// Print a detailed dump (with types) of `"5" !== 5`
var_dump("5" !== 5);
?>
Login to try C/C++/Java/PHP code in the editor
Greater Than और Less Than
> और < दो values को ordinary numeric या string ordering से compare करते हैं, सिर्फ तभी true return करते हुए जब comparison strictly एक-तरफ़ा हो — equal values > और < दोनों में fail होती हैं, यही exactly वजह है कि नीचे equal-or-greater variants exist करते हैं उन cases के लिए जहाँ एक tie count होनी चाहिए।
उदाहरण: Greater Than and Less Than
<?php
// Print a detailed dump (with types) of `5 > 3`
var_dump(5 > 3);
// Print a detailed dump (with types) of `5 < 5`
var_dump(5 < 5);
?>
Login to try C/C++/Java/PHP code in the editor
Greater Than or Equal to और Less Than or Equal to
>= और <= > और < को equality भी accept करने के लिए extend करते हैं, जो आपको 'क्या यह age काफी बड़ी है' या 'क्या यह value अपनी limit तक पहुँच गई' जैसे boundary checks के लिए चाहिए — situations जहाँ boundary value खुद check pass कर जानी चाहिए, सिर्फ इससे strictly आगे की values नहीं।
उदाहरण: Greater Than or Equal to and Less Than or Equal to
<?php
// Declare `$age`, set to `18`
$age = 18;
// Print a detailed dump (with types) of `$age >= 18`
var_dump($age >= 18);
// Print a detailed dump (with types) of `$age <= 17`
var_dump($age <= 17);
?>
Login to try C/C++/Java/PHP code in the editor
Spaceship Operator (<=>)
<=>, spaceship operator, एक three-way comparison को एक expression में condense करता है: यह -1, 0, या 1 return करता है इस आधार पर कि left side छोटा है, equal है, या बड़ा है।
यह specifically custom sort comparison functions लिखना simplify करने के लिए exist करता है, जहाँ अन्यथा आपको तीन अलग if branches चाहिए होतीं।
उदाहरण: Spaceship Operator (<=>)
<?php
// Print `3 <=> 5, "\n"` to the output
echo 3 <=> 5, "\n";
// Print `5 <=> 5, "\n"` to the output
echo 5 <=> 5, "\n";
// Print `8 <=> 5` to the output
echo 8 <=> 5;
?>
Login to try C/C++/Java/PHP code in the editor
===की ज़रूरत होने पर==इस्तेमाल करना, ताकि"0" == falseया"abc" == 0(PHP 8 से पहले) surprising results दें।- किसी comparison में
==के बजाय=लिखना जैसेif ($x = 5), जो 5 assign करता है और हमेशा true होता है। - यह मान लेना कि
<=>का result boolean है, जबकि यह-1,0या1return करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: