Should you come across === before being taught what it is, you’ll likely wonder how it’s different from == or maybe even wonder if it’s a mistake. No, it’s no mistake. They’re similar but different.

== checks if the values are equal. === also checks for equal values and does another check for equal data types. Consider the following code.

$a = 0;
$b = false;
if($a == $b) {
   echo "Equal";
}
if($a === $b) {
   echo "Identical";
}

That would output “Equal” since, in PHP, false and zero are equal. They are not, however, the same type. Zero is an integer while false is boolean. A similar output would occur when comparing 1 and true.

Be aware of this. Your function or database query may return zero which is equal to false. If you simply check that it didn’t return false with !$res for example, that may not always work the way you intended (when $res is zero).

The logical opposites of those are != (not equal) and !== (not equal OR not same type). An example:

$a = 0;
$b = false;
if($a != $b) {
   echo "Not Equal";
}
if($a !== $b) {
   echo "Not Identical";
}
 
$a = "apple";
$b = 0;
if($a != $b) {
   echo "Not Equal";
}
if($a !== $b) {
   echo "Not Identical";
}

The first will display “Not Identical”. The second will also display “Not Identical” (when comparing the string “apple” with zero, they are equal).

That last example demonstrates a good reason to check data types in some situations.