is_null()
The is_null() function is a built-in function in PHP that checks whether a variable is null or not. Null is a special value that represents the absence of a
Introduction
is_null() is a built-in PHP function that returns true when a variable holds the value null, and false otherwise. null is PHP's special "no value" type — it represents a variable that has been explicitly emptied, assigned null, or never given a value. This page covers the syntax, what is_null() does (and doesn't) catch, how it differs from isset() and empty(), and when to reach for the === null comparison instead.
Syntax
is_null(mixed $value): boolIt takes one argument, $value, and returns a boolean:
| Input | Returns |
|---|---|
null | true |
Anything else — 0, "", "0", false, [] | false |
A common source of bugs is assuming "empty" values are null. They are not: is_null() is strict, so 0, the empty string, and false all return false.
Basic example
Note: when echoed, PHP converts true to 1 and false to an empty string, which is why the second line prints nothing. To see the boolean values clearly, use var_dump() instead of echo.
Null is not the same as empty
This is the most frequent misunderstanding. Only null is null — values that merely feel empty are not:
<?php
var_dump(is_null(null)); // bool(true)
var_dump(is_null(0)); // bool(false)
var_dump(is_null("")); // bool(false)
var_dump(is_null(false)); // bool(false)
var_dump(is_null([])); // bool(false)
?>If you want to catch all of those "empty" cases at once, use empty() instead. Use is_null() only when you specifically mean "the value is literally null".
A practical use case
is_null() shines when a value is genuinely optional and null is a meaningful "not provided" marker — for example a database column or an API field that can be missing:
<?php
function greet(?string $middleName): string {
if (is_null($middleName)) {
return "No middle name on file.";
}
return "Middle name: $middleName";
}
echo greet(null) . "\n"; // No middle name on file.
echo greet("Marie") . "\n"; // Middle name: Marie
?>is_null() vs === null
In modern PHP, is_null($var) and $var === null are functionally identical — both are strict null checks:
<?php
$x = null;
var_dump(is_null($x) === ($x === null)); // bool(true)
?>The === null form is generally preferred in modern codebases: it avoids a function call (a small performance edge), reads consistently with other strict comparisons, and works in places where a constant expression is required. Reach for is_null() when you want a named, callable test — for example as a callback: array_filter($items, 'is_null').
Gotcha: undefined variables
is_null() returns true for a variable that was never defined, but it also emits an Undefined variable warning:
<?php
var_dump(is_null($neverSet)); // Warning: Undefined variable ... then bool(true)
?>If the variable might not exist at all, check with isset() first — isset() returns false for both undefined and null variables and never raises a warning. Use is_null() only on variables you know are defined.
Conclusion
is_null() is a precise, strict test for the null value. Use it when null carries a specific meaning in your code and you want to distinguish it from 0, "", or false. For "is this missing or empty in any sense" checks, prefer isset() or empty(), and to inspect a value's actual type see gettype() and PHP data types.