w3resource

PHP: Comparison Operators

Description

In PHP, comparison operators take simple values (numbers or strings) as arguments and evaluate to either TRUE or FALSE.

Here is a list of comparison operators.

Operator Name Example Result
= = Equal $x == $y TRUE if $x is exactly equal to $y
= = = Identical $x === $y TRUE if $x is exactly equal to $y, and they are of the same type.
!= Not equal $x != $y TRUE if $x is exactly not equal to $y.
<> Not equal $x <> $y TRUE if $x is exactly not equal to $y.
!== Not identical $x !== $y TRUE if $x is not equal to $y, or they are not of the same type. 
< Less than $x < $y TRUE if $x (left-hand argument) is strictly less than $y (right-hand argument).
> Greater than $x > $y TRUE if $x (left hand argument)  is strictly greater than $y (right hand argument).
<= Less than or equal to $x <= $y TRUE if $x (left hand argument) is less than or equal to $y (right hand argument).
>= Greater than or equal to $x >= $y TRUE if $x is greater than or equal to $y.

Pictorial presentation of Equal (==) operator

php equal operator

 

Test Equal (==) operator

The following php codes return true though the type of $x and $y are not equal (the first one is integer type and the second one is character type) but their values are equal.

<?php
$x = 300;
$y = "300";
var_dump($x == $y);
?>

Output :

 bool(true)

View the example in the browser

Pictorial presentation of Strict equal (===) operator

php strict equal operator

 

Test Strict equal (===) operator

The following php codes returns false as the strict equal operator will compare both value and type of $x and $y.

<?php
$x = 300;
$y = "300";
var_dump($x === $y);
?>

Output :

bool(false)

View the example in the browser

Pictorial presentation of Not equal(!=)/(<>) operator

php not equal operator

 

Test Not equal (!=) / (<>) operator

The following php codes return false though the type of $x and $y are not equal (the first one is integer type and the second one is character type) but their values are equal.

<?php
$x = 150;
$y = "150";
var_dump($x != $y);
?>

Output of the example

bool(false) 

View the example in the browser

Test Not identical (!==) operator

The following php codes return true though their values are equal but the type of $x and $y are not equal (the first one is integer type and the second one is character type).

<?php
$x = 150;
$y = "150";
var_dump($x !== $y);
?>

Output :

bool(true) 

View the example in the browser

Pictorial presentation of Greater than(>) operator

php greater than  operator

 

Test Greater than(>) operator

The following php code return true as the value of $x is greater than $y.

<?php
$x = 300;
$y = 100;
var_dump($x>$y);
?>

Output :

bool(true) 

View the example in the browser

Pictorial presentation of Greater than or equal (>=)

php greater than or equal operator

 

Test Greater than or equal (>=) operator

The following php codes return true as the value of $x is equal to $y.

<?php
$x = 300;
$y = 100;
var_dump($x>=$y);
?>

Output :

bool(true) 

View the example in the browser

Pictorial presentation of Less than (<) operator

php less than operator

 

Test Less than (<) operator

The following php codes return true as the value of $x is less than $y.

<?php
$x = 100;
$y = 300;
var_dump($x<$y);
?>

Output :

bool(true) 

View the example in the browser

Pictorial presentation of Less than or equal (<=) operator

php less than or equal operator

 

Test Less than or equal (<=) operator

The following PHP codes return false as the value of $x is greater than $y.

<?php
$x = 300;
$y = 100;
var_dump($x<=$y);
?>

Output :

bool(false) 

View the example in the browser

See also

PHP variables

Previous: Arithmetic Operators
Next: Logical Operators



PHP: Tips of the Day

PHP: How to get a variable name as a string in PHP?

You could use get_defined_vars() to find the name of a variable that has the same value as the one you're trying to find the name of. Obviously this will not always work, since different variables often have the same values, but it's the only way I can think of to do this.

Edit: get_defined_vars() doesn't seem to be working correctly, it returns 'var' because $var is used in the function itself. $GLOBALS seems to work so I've changed it to that.

function print_var_name($var) {
    foreach($GLOBALS as $var_name => $value) {
        if ($value === $var) {
            return $var_name;
        }
    }

    return false;
}

Edit: to be clear, there is no good way to do this in PHP, which is probably because you shouldn't have to do it. There are probably better ways of doing what you're trying to do.

Ref : https://bit.ly/37qFimU