PHP is_null: How to Check If Variable is NULL

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

The null in PHP represents a variable with no value. The null is the only possible value of type null. PHP provides a function to check whether the value is null or not. Let’s see how to do that.

PHP is_null

PHP is_null() is a built-in function that finds whether a variable is NULL. A unique 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 If variable is null in PHP

To check if the variable is NULL, use the PHP is_null() function. 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.

Syntax

See the syntax of is_null() function.

is_null( mixed $var ) : bool

Arguments

The variable is a required argument that specifies a variable to be checked.

Example

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. Instead, 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';
  }

See the output.

Casting to NULL

 

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

Let’s see another 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 helpful in some cases, like loaded server scenarios where you have hundreds or thousands of requests arriving per second.

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

That’s it for this example.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.