Triple Equal Operator and NULL in PHP

A PHP variable has a value and a type. In most practice cases we consider only about the value of the variable. But there may be times we have to consider the type of the variable as well.

For an example value NULL (or 0 or whatever of following you likeĀ  to call it) can be assigned to a variable with different types like this.

$x = NULL; NULL data type. Yea it is the real NULL.
$x = 0; Integer data type.
$x = 0.0; Float data type.
$x = FALSE; Boolean data type.
$x = “”; String data type.

You can check the operator ‘===’ (triple equal operator) to check equality of both their values and types simultaneously.

You can have a good idea about this by looking at the following piece of code.

// lets check the equality of NULL and 0 with
// double equal operator
if(NULL == 0) {
	echo "NULL == 0 is TRUE</br>";
}
else {
	echo "NULL == 0 is FALSE</br>";
}

// now lets check the equality of NULL and 0 with
// triple equal operator
if(NULL === 0) {
	echo "NULL === 0 is TRUE</br>";
}
else {
	echo "NULL === 0 is FALSE</br>";
}

This will eventually print the following result.

NULL == 0 is TRUE
NULL === 0 is FALSE

PHP variables including class variables get the NULL value and NULL type by default. So if no one assign them a value it remains NULL.

So in a case you have to check for unused variables (or for “NULL”-ness of a variable) you should always use the triple equal operator (===) with NULL token. Otherwise you may mistakenly treat not NULL things like ‘0’ or empty string (“”) as null that may have been valid data for your application.

This entry was posted in php, Tutorial/Guide and tagged , , , . Bookmark the permalink.

9 Responses to Triple Equal Operator and NULL in PHP

  1. Gromitt says:

    For NULL comparisons, prefer the is_null($foo) built-in PHP function which makes probably more sense, as “NULL” is not really a value (nor a type), but the “absence of a value”.

  2. dimuthu says:

    yea. the is_null($foo) do the same job as $foo === NULL. Thanks for raising that.

  3. Rubayeet says:

    ‘===’ is known as ‘Strict Comparison Operator’. It checks both the values and types of its operands. It is considered a good practice to use it instead of ‘==’. You may take a look at this post:

    http://rubayeet.wordpress.com/2008/04/29/a-simple-php-gotcha/

  4. dimuthu says:

    Hi Rubayeet,
    I agree with you. Simply you have to use === and == according to the application. In fact there can be situation you don’t really care about types (say you expect either string ‘2’ or 2). In these situation you will be using ‘==’.

  5. Ivan says:

    Never use ‘==’! It doesn’t compare data type and it’s slower!

  6. dimuthu says:

    Hi Ivan,
    I haven’t check the execution time of ‘==’. I feel it is anyway speeder than ‘===’ because it doesn’t compare the type. Anyway Should do a test and confirm.

    Thanks
    Dimuthu

  7. dimuthu says:

    Hi Ivan,
    Amazingly you are right. I did a simple loop with 1000000 comparisons for both ‘==’ and ‘===’ with both simple types and class instances. ‘===’ was the faster. Should digg in to the c code to see what s really happening there.

    Thanks
    Dimuthu

  8. Gromitt says:

    Here is the global idea :

    === : IF “types are the same”, THEN IF “values are the same”, THEN return TRUE.

    == : IF “types are not the same”, THEN re-cast “right-hand operator” into the same type as “left-hand operator”, THEN IF “values are the same”, THEN return TRUE.

  9. dimuthu says:

    Thanks @Gromitt,
    That summarize the whole story.

Leave a Reply

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