CodesCompiler
🔍
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact 🚀 Get Started Free

PHP - is_null() Function

PHP Variable Handling is_null() Function

The PHP Variable Handlingis_null()function is used to checks whether a variable is null. It returns true when the variable is null. It returns false if the variable contains any value. This function is useful for determining if a variable is empty.

It helps prevent errors caused by missing values. It can run PHP 4, PHP 5, PHP 7, and PHP 8. It can be used to validate user input, database values, and other information. It is a simple and easy to use PHP function.

Syntax

Below is the syntax of the PHP Variable Handlingis_null()function −

PHP CODE
bool is_null ( mixed $value )

Parameters

This function accepts$valueparameter which is the variable that you want to check.

Return Value

Theis_null()function returns TRUE if the variable is null. And it returns FALSE if the variable has a value.

PHP Version

First introduced in core PHP 4.0.4, theis_null()function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

This program uses the PHP Variable Handlingis_null()function to check whether a variable is null. If the variable is null, it will say “Variable is null”. Otherwise, it states “Variable is not null”. This is a simple example showing how is_null() works.

PHP CODE
<?php

   // Assigning null value

   $var = null; 

   if (is_null($var)) {

      echo "Variable is null"; 

   } else {

      echo "Variable is not null";

   }

?>

Output

Here is the outcome of the following code −

PHP CODE
Variable is null

Example 2

This program checks if an undefined variable is null. As the variable has not been declared,is_null()function returns true. This helps to prevent errors caused by missing variables. It provides the safe execution of PHP code.

PHP CODE
<?php

   if (is_null($undefinedVar)) {

      echo "Variable is null"; 

   } else {

      echo "Variable is not null";

   }

?>

Output

This will generate the below output −

PHP CODE
Variable is null

Example 3

This program checks if an array element is null using theis_null()function. It is useful for working with user input and database records. If the element is null, it will say “Element is null”. Otherwise, it will show “Element is not null”.

PHP CODE
<?php

   // Associative array with a null value

   $data = ["name" => "John", "age" => null]; 

   if (is_null($data["age"])) {

      echo "Element is null"; 

   } else {

      echo "Element is not null";

   }

?>

Output

This will create the below output −

PHP CODE
Element is null

php_variable_handling_functions.htm