php
  1. php-is-null-function

PHP is_null() Function

The is_null() function in PHP is used to check whether a variable is null or not. It returns a boolean value i.e true or false depending upon whether the variable is null or not.

Syntax

bool is_null ( $var )

Example

$var = null;

if (is_null($var)) {
    echo "The variable is null.";
} else {
    echo "The variable is not null.";
}

Output

The variable is null.

Explanation

In the above example, we have initialized a variable $var with a null value. Then we have used the is_null() function to check whether the variable is null or not. Since the $var variable is null, the if condition is true and the statement inside the if block is executed which is "The variable is null.".

Use

The is_null() function is mostly used to check whether a variable is null or not. It is essentially used in conditional statements where the programmer wants to perform a specific set of operations depending upon whether the variable is null or not.

Important Points

  • The is_null() function only checks for null values. If the variable contains any other value (string, integer, boolean, etc.), it will return false.
  • If the variable is not defined, is_null() function will return true.

Summary

  • is_null() function is used to check whether a variable is null or not.
  • It returns boolean values i.e true or false.
  • It is mostly used in conditional statements to perform specific operations based on the variable is null or not.
  • is_null() function only checks for null values and not for any other value.
Published on: