Comparison operators in PHP allow you to compare values and variables. For example, they can be used to check whether a variable has a specific value, for example.
Operator | Name | Explanation |
---|---|---|
$a == $b | Equal | This comparison is true if $a and $b contain the same value. If the types of variables are different, they will be converted. |
$a === $b | Identical | This comparison is true if $a and $b have the same type and content. If one value was of type int and the other was of type String, then false would be returned. |
$a != $b | Not Equal | This comparison is true if $a and $b do not contain the same value. If the types of variables are different, they will be converted. |
$a == $b | Not Identical | This comparison is true if $a and $b are of a different type or value. |
$a < $b | Smaller than | $a must be less than $b. |
$a <= $b | Smaller or equal | $a must be less than or equal to $b. |
$a > $b | Greater than | $a must be greater than $b. |
$a >= $b | Greater or equal then | $a must be greater than or equal to $b. |
Comparison of variables and values
Using the above operators, you can compare variables as well as values in PHP. The result of the comparison is always either true or false. The result of the comparison can either be stored in a variable or used directly in the condition of if statement or loops.
Below is a short example of the comparison operators. If you want to display the value of the comparison, the function var_dump($variable) is used. It is not possible to output a boolean value via echo.
<?php
$int1 = 15;
$int2 = 20;
if ($int1 < $int2) {// compare two variables
echo "Int1 is less than int2 <br />"; }
if ($int1 <= 100) {// comparison of variable and value
echo "Int1 is less than or equal to 100 <br />";}
//storing the comparison in a variable
$animal = "cat";
$string_comparison = ($animal == "cat");
echo 'The value of variable $string_comparison is: <br />';
var_dump ($string_comparison); // output what value the variable has
if ($string_comparison) {
echo "--- The string comparison had the true value ---";
}
?>
The difference between == and ===
In PHP the comparison operators == and === exist to check for equality and != and !== to check for inequality.
The difference between == and ===, that with == only the value is checked, with === additionally the type of the variable is checked. For example, if you put integer 1 in one variable and string "1" in the other variable, then == would return true, but operator === will return false because the types of variables were different.
The following code shows you the differences:
<?php
$integer = 1;
$string = "1";
echo "Result of ==:";
var_dump ($integer == $string);
echo "<br /> Result of ===:";
var_dump ($integer === $string);
?>
The situation is similar with != and !==. The comparison operator != checks if the contents are different. With !== the types of variables are checked first. If these are different, then true is returned, even if the content is same:
<?php
$integer = 1;
$string = "1";
echo "Result of !=:";
var_dump($integer != $string);
echo "<br /> Result of !==:";
var_dump ($integer !== $string);
?>
In the above example, $integer != $string results in false because the content is not different. In contrast, $integer !== $string results in true, because the types of variables were different (string and integer).
When do I use == and when ===?
In most cases, == or != is often used, but in some situations it is necessary to change to === or !==. The function strpos ($text, $searchword) returns the position of the $searchword in $text. If it is not found, false is returned. Now strpos() can also return position 0 if the search term is at the beginning of the text. However, as the following example shows, a comparison with != Would not work here:
<?php
$text = "This is a text";
$searchword = "This";
$position = strpos($text, $searchword);
if ($position == false) {
echo "Your search term was not found in the text";
} else {
echo "Your search word was found at position $ position";
}
?>
The above script erroneously states that the search term was not found. This is because PHP also takes the value 0 as false. The correct one is by using === :
<?php
$text = "This is a text";
$searchword = "This";
$position = strpos ($text, $searchword); i
f ($position === false) {
echo "Your search term was not found in the text";
} else {
echo "Your search word was found at position $position";
}
?>
The if statement is only true if strpos() actually returned false.