Strict Comparison in PHP

In this tutorial you will learn how strict comparison works in PHP, how it is different from standard comparison, and when you need to use it.

Contents


Standard comparison in PHP

You can store different types of data in PHP variables: text strings, numbers, Booleans, arrays, and more.

When you perform an operation with variables, you can mix different types together.

For example, you can use text strings as they were numbers or numbers as they were text strings, depending on the context.

Like this:

$number = 5;
$text = "10";
$sum = $number + $text;
$string = $number . $text;

echo "Numeric sum: " . $sum . "<br>";
echo "String concatenation: " . $string;
Numeric sum: 15
String concatenation: 510

In the above example, $text is used as a number when calculating $sum, and $number is used as a text string when defining $string.

This also applies to comparison operators.

You can compare variables with different types, and PHP decides automatically how to evaluate each variable.

For example:

$number = 5;
$text = "5";

if ($number == $text) {
   echo $number . ' and ' . $text . ' are equal.';
}
5 and 5 are equal.

Strict comparison vs standard comparison

When you compare two variables to check if they are equal, you can use the standard comparison operator or the strict comparison operator.

Standard comparison, or loose comparison, compares two variables regardless of their type.
The standard == operator checks if two variables are equal, and the standard != operator checks if two variables are not equal.

For example:

$number = 5;
$text1 = "5";
$text2 = "7";

if ($number == $text1) {
   echo $number . ' and ' . $text1 . ' are equal.';
}

if ($number != $text2) {
   echo $number . ' and ' . $text2 . ' are not equal.';
}
5 and 5 are equal.
5 and 7 are not equal.

Strict comparison differs from standard comparison because it also considers the variables’ type.

Two variables can be strictly equal only if they have the same type.

In a nutshell, strict comparison works like loose comparison, but it always considers variables with different types as not equal.

The strict equality operator is ===
The strict inequality operator is !==

For example:

$number = 5;
$text = "5";

if ($number === $text) {
   echo $number . ' and ' . $text . ' are strictly equal.';
}

if ($number !== $text) {
   echo $number . ' and ' . $text . ' are not strictly equal.';
}

if ($number === 5) {
   echo $number . ' and 5 (number) are strictly equal.';
}
5 and 5 are not strictly equal.
5 and 5 (number) are strictly equal.

When you should use strict comparison

Standard comparison works fine most of the time.
However, in some cases it can lead to unexpected results.

For instance, the Boolean false value and the number 0 are considered equal by the standard comparison operator. This is not always the desired behavior.
Numeric strings also can lead to unexpected comparison results.

Let’s see a couple of examples:

if (0 == false) {
   echo "0 and false are equal.<br>";
}

if (123 == "  123  ") {
   echo "123 and '  123  ' are equal.<br>";
}

if ("001" == "1") {
   echo "001 and 1 are equal.<br>";
}

if ("200" == "2E2") {
   echo "200 and 2E2 are equal.<br>";
}
0 and false are equal.
123 and ' 123 ' are equal.
001 and 1 are equal.
200 and 2E2 are equal.

As you can see, there are cases where you probably want to use strict comparison to make sure that the comparison results are what you expect.

For instance, if you want to compare two text strings and make sure they are equal, strict comparison is a better choice.

When you must use strict comparison

As a general rule, using strict comparison makes your code more solid and readable. The downside is that you must pay attention to the type of your variables, because you cannot rely on PHP automatic conversion.

On top of that, there are two cases where strict comparison is mandatory and you must use it:

1. When you need to make sure that two text strings are exactly the same.

As you saw in the last example, PHP considers two text strings as equal if they are both numeric strings and represent the same number.

If you must make sure that the strings are identical, then you must use strict comparison.

For example:

$secret = "123456";
$input = "0123456";

echo "Standard comparison:<br>";
if ($input == $secret) {
   echo "Welcome!";
}
else {
   echo "Sorry, wrong code.";
}

echo "<br><br>Strict comparison:<br>";
if ($input === $secret) {
   echo "Welcome!";
}
else {
   echo "Sorry, wrong code.";
}
Standard comparison:
Welcome!

Strict comparison:
Sorry, wrong code.

2. When you must separate 0 from false.

Some PHP functions can return an integer number, including 0, as well as the Boolean false.

For example, the string function strpos() returns the position of a string inside another string if the string is found, or false if the string is not found.
The first position is 0, so you need to use strict comparison to distinguish 0 from false.

For example:

$fruits = 'apple, peach, banana';
$apple = 'apple';
$pos = strpos($fruits, $apple);

echo "Standard comparison:<br>";
if ($pos == false) {
   echo "apple not found.";
}
else {
   echo "apple found at position: " . $pos;
}

echo "<br><br>Strict comparison:<br>";
if ($pos === false) {
   echo "apple not found.";
}
else {
   echo "apple found at position: " . $pos;
}
Standard comparison:
apple not found.

Strict comparison:
apple found at position: 0

Conclusion

In this tutorial you learned the differences between Standard comparison and Strict comparison in PHP.

You also saw how the standard comparison operator can provide unexpected results, especially with numeric strings, and how using strict comparison can be a better choice in some cases.

Finally, you learned when using strict comparison is mandatory to avoid coding mistakes.

Be sure to leave a comment if you have any questions.

Alex

When you code in PHP, do you still feel like a beginner?

Get more confident about your PHP skills with 5 learning strategies. Get the free PDF now.

Leave a Comment