How To Check Variable Is NULL in PHP | PHP is_null() Tutorial Example

Like Tweet Pin it Share Share Email
How To Check Variable Is NULL in PHP | PHP is_null() Tutorial Example

How To Check Variable Is NULL in PHP | PHP is_null() Tutorial Example Example is today’s topic. PHP is_null() function finds whether a variable is NULL. A special NULL value represents the variable with no value. The NULL is the only possible value of type NULL.

The variable is considered to be null if:

  1. It has been assigned a constant NULL.

  2. It has not been set to any value yet.

  3. It has been unset().

How To Check Variable Is NULL in PHP

The is_null() function is used to test whether the variable is NULL or not. The is_null() function returns TRUE if the variable is null, FALSE otherwise.

There is only one value of type null, and it is the case-insensitive constant NULL.

See the syntax of is_null() function.

is_null( mixed $var ) : bool

See the following code example.

<?php

// app.php

  $var = null;
  if(is_null($var)) {
    echo 'The variable is NULL';
  }
  else {
    echo 'The variable is not NULL';
  }

The output is following.

How To Check Variable Is NULL in PHP

You can use the null or NULL, and it is case insensitive so that the output will be the same.

Casting to NULL

Casting a variable to be null using (unset) $var will not remove the variable or unset its value. It will only return a NULL value. 

An empty array is converted to null by non-strict equal ‘==’ comparison. Use is_null() or ‘===’ if there is possible of getting empty array.

See the following code.

<?php

// app.php

  $a = array();

  if($a == null) {
    echo 'First NULL';
  }
  if($a === null) {
    echo 'Second NULL';
  }
  if(is_null($a)) {
    echo 'Third NULL';
  }

Now, the output is following.

Casting to NULL

So, the Non Strict Comparison ‘==’ returns bool(true).

PHP is_null() second example

See the following code.

<?php

// app.php

$foo = NULL;
var_dump(is_null($foo));

See the output.

How To Check Variable Is NULL in PHP | PHP is_null() Tutorial Example

Performance

In PHP 7 (phpng), is_null is drastically faster than ===, although the performance difference between the two is far smaller.

PHP 5.5.9
is_null – float(2.2381200790405)
===     – float(1.0024659633636)
=== faster by ~100ns per call

PHP 7.0.0-dev (built: May 19 2015 10:16:06)
is_null – float(1.4121870994568)
===     – float(1.4577329158783)
is_null faster by ~5ns per call

Using === NULL instead of is_null() is useful in some cases like loaded server scenarios where you have hundreds or thousands of requests arriving per second.

Saving microseconds on the lot of “simple” operations in an entire PHP execution chain usually results in being able to serve the more pages per second at the same speed or lowering your CPU usage. People usually write very bad and slow code.

Finally, How To Check Variable Is NULL in PHP | PHP is_null() Tutorial Example post is over.

Comments (0)

Leave a Reply

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