extensions development for Joomla!™


The "not so logical" comparison of 0 to null

When evaluating code, it is important to know exactly with which types of data we are working with and also to which type of software is it related, because sometimes, things aren't that logical as we might think and the results might not be the expected or desired.

Let's perform a couple of tests, using JavaScript and PHP, and see the results on each of these scripting languages, while comparing zero to null. In each example, we'll compare 0 (zero) to null using different available methods:

JavaScript (ECMAScript 5.2)

// type of 0 is number
// type of null is null

0 === null; // returns false
0 == null; // returns false
0 == +null; // returns true (type of null is now number)
0 === +null; // returns true (type of null is now number)
0 >= null; // returns true
0 <= null; // returns true

In the above comparisons, we can notice something - zero is not equal to null when performing the strict equality (===) or just the simple equality (==) operations. Then, when performing relational operations (>= or <=) we notice that the result of these evaluations always returns true, which means that, in plain words, zero is greater then or equal to null, and zero os less then or equal to null. Now, compare these results with the first evaluations where we concluded that zero is not equal to null.

On ECMAScript 5.2, in the Abstract Equality Comparison Algorithm, while performing the comparison x == y, there is no condition when Type(x) is 0 and Type(y) is null, thus, it skips to step 10 where the comparison returns false. Zero is then not equal to null.

Also notice that, when we use the unary operator +null, null is converted by JavaScript to type "number" - the same if we used the prototype object Number(null). Then, the comparison is made as at 1.c.iii where both types are numbers and with the same value.

PHP (on 5.3.1)

// 0 is type integer
// null is type NULL

is_null(0); // returns false
is_null(NULL); // returns true
0 == null; // returns true
0 == +null; // returns true
0 === null; // returns false
0 === +null; // returns true
0 >= null; // returns true
0 <= null; // returns true

In the PHP tests, we notice some differences between the same comparisons with the ones performed in JavaScript, although the types at the beggining are the same - 0 is integer and null is NULL.

The main big difference, that we can automatically motice (even by the highlighted lines) is that when performing the relaxed equality comparison in PHP, 0 == null the comparison returns true while in JavaScript it returns false.

In summary

It's important not to take by granted the logical behaviors of each language, just because, at the first sight, we believe that the results should logically be the same. This is just one of the several cases where the final result is different from the expected.

If we would write the JavaScript conclusion just in words it will sound weird, but it's true:
"Zero is not equal to null. It is also not greater than null or less then null, but... zero is greater or equal than null and less than or equal to null."

Shopping cart

The cart is empty


Extension added to cart! x
Back to Top