PHP – Loose Comparison (==) VS Strict Comparision (===)

In PHP, there are two options when comparing values in a condition. These comparison options are loose comparison (==) and strict comparison (===). PHP is a loosely typed language which means that variables can be assigned different data types during execution of the application and different data types can be compared in a conditional. This is because when a variable is declared in PHP the data type is determined by how the variable is used and the data type of the variable can continue to change because type juggling can happen depending on how the variable continues to be used.

PHP has the following comparison operators to test if two operands are equal:

Loose Comparison (==)

In PHP, variables of different data types can be compared using the loose comparison operator which is two equal signs (==). If two operands of different types are compared using loose comparison then there is an attempt to convert one or both of the operands and then compare them. The result of the conditional after conversion of an operand can sometimes lead to bugs or security exploits.

Strict Comparison (===)

In PHP, variables can have the data type and value compared using strict comparison which is three equal signs (===). With strict comparison, no type conversion is performed if the variables are different data types. If the variables are different data types then false is returned. If the variables are the same data type then the variable’s values are compared. This is known as checking if two variables are identical. Variables are identical if each variable has the same data type and value.

Results Of Loose Comparison (==) And Strict Comparison (===)

The PHP interactive shell can be used to quickly compare different data types and immediately see if the result is true or false. The PHP interactive shell can be accessed by typing php -a at the command line.

Comparing NULL To A String

When a variable of the NULL data type is compared to a variable of the string data type then the NULL value will be converted to an empty string “”.

The below screenshot from a PHP interactive shell shows the result of casting NULL to a string will convert NULL to “”. The screenshot then shows the results of comparing NULL to “” using loose comparison and strict comparison.

Loose Comparison (==) Result: The NULL data type is converted to an empty string “” and then compared to the string “” and true is returned.
Strict Comparison (===) Result: The operands are different data types so false is returned.

Comparing NULL To An Integer

When a variable of NULL data type is loosely compared to an integer then both variables are converted to the boolean data type and then there values are compared.

The below screenshot shows the result of converting NULL to a boolean and converting an integer of 0 to a boolean. The screenshot then shows the results of comparing NULL to 0 using loose comparison and strict comparison.

var_dump of NULL compared to string

Loose Comparision (==) Result: The NULL data type is converted to a boolean value of false and the integer data type of 0 is converted to a boolean value of false. The operands of false and false are then compared and true is returned.
Strict Comparison (===) Result: The operand of NULL is the NULL data type and the operand of 0 is the integer data type so false is returned.

Comparing A Boolean To An Integer

When a variable of a boolean data type is loosely compared to a variable of the integer data type then the integer is converted to a boolean and the values are compared.

The below screenshot shows the result of converting the integer 1 to a boolean. The screenshot then shows the result of loosely comparing true to 1 and strictly comparing true to 1.

var_dump of bool compared to int

Loose Comparison (==) Result: The integer data type of 1 is converted to the boolean true and then the values are compared and true is returned.
Strict Comparison (===) Result: The operands are different data types so false is returned.

Another example is comparing a boolean to the integer 0.

The below screenshot shows the result of converting the integer 0 to a boolean. The screenshot then shows the result of loosely comparing false to 0 and strictly comparing false to 0.

var_dump of bool compared to int

Loose Comparison (==) Result: The integer data type of 0 is converted to the boolean true and then the values are compared and true is returned.
Strict Comparison (===) Result: The operands are different data types so false is returned.

Comparing A Boolean To A String

When a variable of a boolean data type is loosely compared to a variable of the string data type then the string is converted to a boolean and the values are compared.

The below screenshot shows the result of converting the string “test” to a boolean. The screenshot then shows the result of loosely comparing true to “test” and strictly comparing true to “test”.

var_dump of bool compared to string

Loose Comparison (==) Result: The string data type of “test” is converted to the boolean true and then the values are compared and true is returned.
Strict Comparison (===) Result: The operands are different data types so false is returned.

Comparing A String To An Integer

When a variable of a string data type is loosely compared to a variable of the integer data type then the string is converted to an integer and the values are compared.

The below screenshot shows the result of converting the string “5” to an integer. The screenshot then shows the result of loosely comparing 5 to “5” and strictly comparing 5 to “5”.

var_dump of numeric string compared to int

Loose Comparison (==) Result: The string data type “5” is converted to an integer of 5 and then compared to the integer 5 and true is returned.
Strict Comparison (===) Result: The operands are different data types so false is returned.

The conversion of strings to integers can have interesting results. The below screenshot shows the result of converting the string “5 dogs and 6 cats” to an integer is the integer 5. The screenshot then shows the result of loosely comparing the integer 5 to the string “5 dogs and 6 cats” and strictly comparing the integer 5 to the string “5 dogs and 6 cats”.

var_dump of numeric string compared to int

Loose Comparison (==) Result: The string data type “5 dogs and 6 cats” is converted to an integer of 5 and then compared to the integer 5 and true is returned.
Strict Comparison (===) Result: The operands are different data types so false is returned.

The below screenshot shows the result of converting the string “test” to an integer 0. The screenshot then shows the result of loosely comparing the string “test” to the integer 0 and strictly comparing the string “test” to the integer 0.

var_dump of string compared to 0

Loose Comparison (==) Result: The string data type “test” is converted to an integer of 0 and then compared to the integer 0 and true is returned.
Strict Comparison (===) Result: The operands are different data types so false is returned.

Conclusion

The above examples show that making comparisons with loose comparison (==) can sometimes cause unexpected results. When in doubt, it is best to use strict comparison (===) when doing comparisons to check that the operands are identical which means that the operands are the same data type and have the same value.


References:
https://www.php.net/manual/en/language.operators.comparison.php
https://www.php.net/manual/en/types.comparisons.php
https://www.php.net/manual/en/language.types.type-juggling.php
https://www.youtube.com/watch?v=SptQ2LhtxnE
https://www.copterlabs.com/strict-vs-loose-comparisons-in-php/
https://stackoverflow.com/questions/80646/how-do-the-php-equality-double-equals-and-identity-triple-equals-comp

Leave a Comment

Your email address will not be published. Required fields are marked *