This operators are represented by:
|==||Equal||$a == $b||If the variable $a is equal with $b will return true|
|===||Identical||$a === $b||If the variable $a is equal with $b and they are both the same data type (like both string, integer, bool etc.) will return true|
Beside the fact that both operators are checking if the variables are equal, the 'Identical' operator check if they are also the same data type, like integer, string or bool.
So, for PHP the string "1" is equal with the number 1, even though they are different data types, to be sure that they are also the same data type you need to use the 'Identical' operator. Also, the number 0 (zero) is equal with the boolean value FALSE and with an empty string.
<?php $a = 20; $b = '20'; $c = 0; $d = false; var_dump($a == $b); // returns bool(true), so it's true var_dump($a === $b); /* returns bool(false), because even though the value is 20 in both variables, the one from the left is integer and the one in from the right is string */ var_dump($c == $d); // returns bool(true) var_dump($c === $d); // returns bool(false) ?>
The PHP var_dump function is used to display a variable or value, same as with echo, but it will give more information about the structure and the data type of the variable.
What will be the output of the comparison: 1 == false ?
Not equal, Not identical
As their names says, they check if two variables or value are not equal, and for the identical if they also have same data type. They are oposite to '==' and '==='.
|!=||Not equal||$a != $b||Wil return true if $a is not equal to $b|
|<>||Not equal||$a <> $b||Wil return (same as !=) true if $a is not equal to $b|
|!==||Not identical||$a !== $b||Wil return true if $a is not equal to $b, and they are not the same data type (like string, integer etc.)|
What value will this statement return: '0' !== false ?
Greater than, Less than / Or equal to
This operators are:
|>||Greater than||$a > $b||If $a is greater than $b returns true|
|<||Less than||$a < $b||If $a is less than $b returns true|
|>=||Greater than or equal to||$a >= $b||If $a is greater than or equal to $b returns true|
|<=||Less than or equal to||$a <= $b||If $a is less than or equal to $b returns true|
To make their role more explicit please check the example bellow:
<?php $a = 10; $b = 7; $c = 10; var_dump($a > $b); //will output true var_dump($a < $b); //will output false var_dump($a >= $b); //will output true //since $a it's bigger than $b var_dump($a <= $b); //will output false //since $a it's neither less than $b or equal to $b var_dump($a >= $c); //will output true //since they are equal var_dump($b >= $c); //will output true //since $b it's neither bigger than $c or equal to $c ?>
What will be output of the following comparison:
<?php $a = -10; $b = 0; var_dump($a <= $b); ?>
The spaceship operator <=>, like $a <=> $b, was introduced in PHP 7. It returns -1 if the variable from the left is smaller, 0 if the variables are equal, and 1 if the variable from the left is bigger.
<?php $a = 10; $b = 5; echo $a <=> $b; //will return 1 //since $a is bigger than $b $c = 12; $d = 12; echo $c <=> $d; //will return 0 //since $c is equal to $d $e = 7; $f = 9; echo $e <=> $f; //will return -1 //since $e is smaller than $f
What is the output of the following script?
<?php echo 12 <=> 9;