PHP Comparison Operators, or relational operators, test some type of relation between two values. We’ve seen a few comparison operators in math, such as greater-than and less-than; in PHP, we have a few more comparison operators at our disposal.
The relational operators that we’re going to be looking at are:
- Greater-than: >
- Greater-than-or-equal-to: ≥
- Less-than: <
- Less-than-or-equal-to: ≤
- Equal-to: ==
- Identical-to: ===
- Not-equal-to: != or <>
- Not-identical-to: !==
- Spaceship operator: <=>
The comparison operator, whichever one you choose, will evaluate to either true or false. If we say, 3 > 2, what we’re really saying is:
Is 3 greater than 2?
If we can say YES than that evaluates to a true statement. If we say NO, than that evaluates to a false statement.
Let’s start by assigning a few values to some variables. Remember, single equal sign means assignment; double equal sign means equality.
<?php$a = 10;
$b = 5;
$c = "10";?>
We’ll use the var_dump() function to help us figure out whether the expression will evaluate to true or false; it will just dump the result of the expression.
Let’s start with the equality operator, ==. We want to see whether $a equals $b. Is 10 equal to 5? No, so it’s false.
<?php
var_dump( $a == $b ); // false
?>
Let’s note the different data types for each of our variables.
- $a stores an integer
- $b stores an integer
- $c stores a string
Let’s try the equality operator again, this time between $a and $c. It comes out to true? Why? PHP looks at the string “10” and sees that it contains a number inside; it’s a numeric string. It loosely compares the two values and sees that they do in fact match.
The equality operator looks at only the value, not the data type.
<?php
var_dump($a == $c); // true
?>
If the data types are important, the identity operator, ===, is used. It compares both the value and the data type. If they don’t match, the expression will return false.
<?php
var_dump($a === $c); // false
?>
Similarly, if you want to see if the two values are not equal, you can use the “not-equal-to” operator (!=). If the two values are not equal, it returns true. Remember, what we’re asking is: is 10 not equal to 5? That’s correct, 10 does not equal 5, so that is a true statement.
<?php
var_dump($a != $b); // true
?>
You can also use <> to test for inequality.
<?php
var_dump($a <> $b); // true
?>
The not-equal-to operator tests only the values of the expression, not the data type. To test the data types along with the values, we would use the not-identical-to operator (!==).
<?php
var_dump($a != $c); // false
var_dump($a !== $c); // true
?>
Greater-than, less-than, greater-than-or-equal-to, and less-than-or-equal-to are straight forward. I’m sure that you’ve seen these operators in action at some point in your life.
<?php
var_dump($a > $b); // true
var_dump($a < $b); // falsevar_dump($a >= $c); // true
var_dump($a <= $c); // true
?>
There is no greater-than-and-identical-to operator or less-than-and-identical-to-operator.
The last operator is called the spaceship operator (<=>). It returns 0 when the values are equal, 1 when the right side is greater than the left side, and -1 when the left side is greater than the right side.
- 0 when equal
- 1 when right is larger
- -1 when left is larger
<?php
var_dump($a <=> $b); // -1
?>
Although we looked at integer values, we can compare any data type. For example, we can compare $a to the boolean value true. We get a weird result: true. Why? Any integer, except zero, when compared to true, is equal to true. This can cause some issues if you accidentally pass a number into a conditional statement, like the if statement. We’ll look at that later.
<?php
var_dump($a == true); // true
var_dump($a === true); // falsevar_dump(4.5 > 2); // true
var_dump("Dino" == "dino"); // false
var_dump("Dino" == "Dino"); // true
?>