Comparing variables in PHP

Posted on December 13, 2014, 12:00 am by

In general it isn't a best practice to compare apples with nuts. But if you really want to do this, you can, but this doesn't mean that you will get the same result as another person would expect to.
The same problem is in programming, especially in PHP, which let's you compare anything you want. And, for that reason, in some cases you will get some interesting results, I would say unexpected.
For example, one day I saw a simple comparison between a string with some random characters, something similar to a md5 result, on one side and a 0 on the other side. I don't know what was the purpose for that comparison and I don't know if it matters anyway.

$a = md5('abcd');
$b = 0;
if ($a == $b) {
    echo 'EQUAL';
} else {
    echo 'DIFFERENT';

What do you think it will be the output in the above case?
And what will be the output for the one below? I have only changed the string on which the md5 is applied.

$a = md5('abc');
$b = 0;
if ($a == $b) {
    echo 'EQUAL';
} else {
    echo 'DIFFERENT';

Let's see these examples in more detail.
In the first example we will have to compare:
"e2fc714c4727ee9395f324cd2e7f331f" vs 0
and in the second example:
"900150983cd24fb0d6963f7d28e17f72" vs 0

How would you compare these values?
If I'm ignoring all I know about PHP, I would say without hesitation that in both cases the values are different.
And I would be right for the second one, but for the first example the result is a little bit difficult to comprehend, as the output is "EQUAL".
What tricks do I have to do for "e2fc714c4727ee9395f324cd2e7f331f" to become equal to 0?!

To compare these values in the way that PHP "thinks", I have to convert one of the value to the type of the other, so the values would have the same type and the comparison would be simple.

If I convert 0 to string, in the end I would have to compare "e2fc714c4727ee9395f324cd2e7f331f" with "0" and the values would be DIFFERENT, but this isn't the result returned by PHP.

So, I will convert "e2fc714c4727ee9395f324cd2e7f331f" to integer, using type casting (int)"e2fc714c4727ee9395f324cd2e7f331f", and the result is 0. Now I have to compare 0 with 0, and now the values are EQUAL, the same as PHP's result.

This is the reason why I am trying to avoid comparisions of this kind.
If you really want to get the expected result, you must compare both the value and the type using the identity operator (===).

If you want to go deeper you will find more and more interesting examples.
Let's say, in general if a = b and b = c, then you can say that a = c.

if (TRUE == "a")    (this expression is evaluated to TRUE)
and ("a" == 0)        (this expression is evaluated to TRUE)
then (TRUE == 0)    (this expression is evaluated to FALSE)

And another one, in general if a < b and b < c, then a < c.

$a = INF; $b = array(); $c = (object)array();
if ($a < $b)        (this expression is evaluated to TRUE)
and ($b < $c)        (this expression is evaluated to TRUE)
then ($a < $c)        (this expression is evaluated to FALSE)

The says that, when you compare a number with a string, the string is converted to a number first and the comparison will be performed between the numbers. There you can either find an explanation how the comparison will be performed between various types.

Leave a Comment:


Blog Search

Popular Blog Categories


Want to be informed about latest posts? Subscribe to our newsletter