The meaning of null…

It is interesting to note that while programming there are many things we take for granted without fully understanding its implications. One of the most common in my opinion is the NULL data type. So when we say a variable is of data type INT it either contains or points to an integer value, in the same way we can say that a variable of data type NULL contains or points to the constant NULL.

The interesting part of this analogy is that we can very easily comprehend the meaning of an integer but not so quite with NULL. So for $a to be an integer we say $a = 5; In the same way if we want $a to be NULL we can write $a = NULL;

So the next question is, why and when do use NULL. The answer is simple. When you do not want to commit to a type. If you have $a = NULL; and you try to echo out $a it will not print anything. There is nothing to print. So, a NULL data type does hold a value. It is simply a placeholder for a future value and hence a data type. You also need to understand that the null character does not make a NULL variable. So $a = “”; is a data type of string and not NULL.

Like other ‘is_’ functions is_null will tell you whether a variable is of the NULL type. Because it is a valid data type it works like any other type. NULL === NULL will give you a true. NULL === ” will give you a false. isset() will give you a false as no value has been set. In a function that does not have a return statement it returns a NULL. One common place where we use NULL is in properties of a class.

Consider the following:

class someClass{
    private $a;
    private $b = null;
    private $c = NULL;

    function printVars(){
        var_dump($this->a, $this->b, $this->c);
    }
}

$o = new someClass();
$o->printVars();

The result is
NULL
NULL
NULL.

So when you write ‘private $a;’ you are indirectly implying that it is NULL because you have not set a value. Remember PHP is not a typed language so it only knows the data type from the context or an explicit value. When you do $a = 5 it knows that you are talking about an INT. The other place it is used is in function parameters.

function addSelf($a = null){
    //do stuff
}

What this implies that $a is an optional parameter. If nothing is passed in the function should still work. However it seems that in this case it implies that you do not know what type of data will be passed in if any.

So we do something like this:

function addSelf($a = null){
    if(!is_string){
        trigger_error('Should be a string');
    }
    //do stuff
}

This is not very good practice because it defeats the concept of default parameter. As you can see $a is not optional it is required and should be a string.

No harm in doing this:

function addSelf($a){
    if(!is_string){
        trigger_error('Should be a string');
    }
    //do stuff
}

This is my take on the NULL data type. I encourage you to read the manual at http://php.net/manual/en/language.types.null.php.

The behavior of null is described here:
http://php.net/manual/en/types.comparisons.php

As you can NULL is a data type and has its uses. Like they say there is a time and a place for everything.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s