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

PHP Relational Operators

Relational operators ऐसे questions पूछते हैं जिनका yes-or-no answer होता है, जैसे 'क्या यह उससे बड़ा है?' या 'क्या ये दोनों same हैं?'। यह दो runners compare करने वाले referee जैसा है।
Syntax
php
$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
<?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);
?>

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
<?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);
?>

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
<?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);
?>

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
<?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);
?>

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
<?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;
?>
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. === की ज़रूरत होने पर == इस्तेमाल करना, ताकि "0" == false या "abc" == 0 (PHP 8 से पहले) surprising results दें।
  2. किसी comparison में == के बजाय = लिखना जैसे if ($x = 5), जो 5 assign करता है और हमेशा true होता है।
  3. यह मान लेना कि <=> का result boolean है, जबकि यह -1, 0 या 1 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.