Definition and Usage
- PHP Version
- 4+
The is_null() function checks whether a given value is NULL.
It returns true if the value passed as an argument is NULL; otherwise, it returns false.
The NULL type is a data type that indicates a variable has not been defined or has no assigned value.
A variable is also assigned the NULL type when the value null is explicitly assigned to it. There is only one possible value of the NULL type, which is the case-insensitive constant null.
Both the NULL type and the value null are case-insensitive.
The NULL type is identical to the Null type or the null type,
and the null value is identical to the Null value or the NULL value.
For convenience in distinguishing between types and values, they have been and will continue to be referred to as the 'NULL type' and the 'null value' throughout this content.
Basic Example
/* NULL type */
var_dump(is_null(null)); // bool(true) => Case-insensitive
var_dump(is_null(Null)); // bool(true) => Case-insensitive
var_dump(is_null(NULL)); // bool(true) => Case-insensitive
/* Unassigned variable */
$a;
var_dump(is_null($a)); // bool(true) => The value of an unassigned variable is null
/* Empty string or whitespace string */
var_dump(is_null('')); // bool(false)
var_dump(is_null(' ')); // bool(false)
/* Empty array */
var_dump(is_null(array())); // bool(false)
var_dump(is_null([])); // bool(false)
/* boolean */
var_dump(is_null(true)); // bool(false)
var_dump(is_null(false)); // bool(false)
Note:
The gettype() function returns the data type of the given value as a string.
Syntax
is_null(mixed $value): bool
Parameters
$value |
Required. The value to check. |
|---|
Return Values
If the value passed as an argument is NULL, the function returns true; otherwise, it returns false.
Things to Keep in Mind
There are several important points to be aware of when using the is_null() function.
Distinguishing Between Empty Strings and null Values
An empty string ('') or a whitespace string (' ') is considered a string type, but it is not a null value. You should take care to distinguish between empty strings and null values during processing.
$empty_string = '';
var_dump(is_string($empty_string)); // bool(true)
var_dump(is_null($empty_string)); // bool(false)
$null_value = null;
var_dump(is_string($null_value)); // bool(false)
var_dump(is_null($null_value)); // bool(true)
Code Explanation
The is_string() function checks whether a given value is a string.
It returns true if the value passed as an argument is a string; otherwise, it returns false.
Distinguishing Between Empty Arrays and null Values
An empty array ([] or, array()) is considered an array type, but it is not a null value. You should take care to distinguish between empty arrays and null values during processing.
$a = array(); // Empty array using array()
$b = []; // Empty array using [] => Introduced in PHP 5.4+
var_dump(is_array($a)); // true
var_dump(is_array($b)); // true
var_dump(is_null($a)); // false
var_dump(is_null($b)); // false
Code Explanation
The is_array() function checks whether a given value is a string.
It returns true if the value passed as an argument is an array; otherwise, it returns false.
Uninitialized or Undefined Variables Have a null Value
In PHP, variables that have not been declared or assigned a value enter an undefined state; when these variables are referenced, their value is considered null.
$a; // No value assigned to $a (Uninitialized)
var_dump(is_null($a)); // true => $a is undefined (Accessing it may trigger an "Undefined variable" Warning)
var_dump(is_null($b)); // true => $b is undefined (Accessing it may trigger an "Undefined variable" Warning)
Potential for Misconception
The is_null() function most clearly reveals your code's intent when you want to check if a variable's value is null.
However, since using it on an undefined variable will trigger a PHP Warning, it is best suited for checking values in situations where you are certain the variable has already been declared.
If the declaration of the variable itself is uncertain, it is safer to use isset() or empty(), which do not trigger a Warning.
Additional Explanation
The isset() function returns true if a variable is declared and its value is not null.
If the variable exists, it returns true even if the value is empty; if the variable is not declared or holds a null value, it returns false.
Additional Explanation
The empty() function returns true if a variable is declared and its value is "empty."
An "empty" value refers to values such as 0, '', null, false, array(), and so on.
$var = null;
/* Scenario 1: When the variable is definitely declared (is_null is recommended) */
// Checking the value of a variable passed as a function argument or declared above
if (is_null($var)) {
echo 'The value of the variable is explicitly null.';
}
/* Scenario 2: When it is uncertain if the variable has been declared */
// Checking external data (like $_GET, $_POST, etc.) or conditionally generated variables
if (!isset($var)) {
echo 'The variable is either undefined or its value is null.';
}
Practical Examples
The is_null() function is useful in explicit cases where a specific operation returns null or when a particular action must be performed only when a value is null. In other words, it is used to make it easier for developers to recognize that null values are being explicitly handled within the code.
When a Function Explicitly Returns null
function getValue($condition) {
if ($condition) {
return null; // Explicitly return null to represent 'no value'
}
return 'someValue';
}
$result = getValue(true);
// While isset($result) could be used, is_null clearly reveals the intent of 'checking for null'
if (is_null($result)) {
echo 'The function explicitly returned null.';
} else {
echo 'The function returned a valid value: ' . $result;
}
The example above demonstrates a case where the getValue() function explicitly returns null based on $someCondition.
References
See also
- PHP gettype() Function – Get the Data Type of a Variable as a String
- PHP is_int() Function – Checking Whether a Value Is an Integer
- PHP is_float() Function – Checking Whether a Value Is a Float
- PHP is_numeric() Function – Checking Whether a Value Is a Number or a Numeric String
- PHP is_string() Function – Checking Whether a Value Is a String
- PHP is_array() Function – Checking Whether a Value Is an Array
- PHP is_bool() Function – Checking Whether a Value Is a Boolean
- PHP is_object() Function – Checking Whether a Value Is an Object
- PHP is_resource() Function – Checking Whether a Value Is a Resource
- PHP isset() and empty() Functions – Concepts, Usage, and Key Differences
- PHP Superglobals – Simple and Essential Usage Guide