Signature
is_null(mixed $value): boolPurpose
is_null() answers a single question: is this value exactly null? It returns a boolean, so you can use it directly in conditions, ternaries and callbacks. In PHP, null represents the absence of a value. Functions often return it to mean "nothing found", and nullable parameters and properties use it for "not provided".
The check is strict. A value is only reported as null if it really is the null value. Zero, an empty string, false and an empty array are all different things, and is_null() treats them that way.
Signature
Parameters and defaults
$valueis the only parameter. It accepts any type (mixed) and has no default, so you must pass exactly one argument.
The value is passed by value. The function does not change the variable you give it.
Return value and failure cases
The function returns true when the value is null and false for everything else. It never throws a TypeError because of the type of the argument, since any type is accepted.
One case deserves care: passing a variable that was never defined. The engine still has to read that variable to pass it, so PHP reports an undefined variable warning, and the function then sees null and returns true. The same happens when you read a missing array key as the argument. The result is technically correct, but the warning shows that the code probably has a bug or a missing check. If you want to test for "might not exist", isset() or the null coalescing operator is the better tool.
Example 1: what counts as null
This loop shows that only the null value gives true. Other "empty-looking" values do not.
$values = [null, 0, "", false, [], "null"];
foreach ($values as $value) {
echo json_encode($value), " => ", var_export(is_null($value), true), "\n";
}null => true 0 => false "" => false false => false [] => false "null" => false
The string "null" is just text, so it is not null either.
Example 2: is_null() compared with isset()
A key may exist in an array and still hold null. The related checks answer different questions.
$data = ['name' => 'Ada', 'middle' => null];
echo json_encode([
'middle_is_null' => is_null($data['middle']),
'middle_isset' => isset($data['middle']),
'missing_isset' => isset($data['missing']),
'key_exists' => array_key_exists('middle', $data),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"middle_is_null":true,"middle_isset":false,"missing_isset":false,"key_exists":true}
isset() returns false for a null value and for a missing key, so it cannot tell them apart. array_key_exists() reports whether the key is present, even when its value is null.
Example 3: handling a nullable return value
A lookup function can return null to say that nothing was found. The caller then branches on that result.
function findName(array $names, int $id): ?string
{
return $names[$id] ?? null;
}
$names = [1 => 'Ada', 2 => 'Grace'];
foreach ([1, 3] as $id) {
$name = findName($names, $id);
echo is_null($name) ? "ID $id: not found" : "ID $id: $name", "\n";
}ID 1: Ada ID 3: not found
Because an empty string is a valid name here, a loose check like if (!$name) would be wrong. is_null() keeps the meaning precise.
Edge cases
is_null($x)gives the same result as$x === null. Many developers prefer the comparison because it is shorter, whileis_null()is handy where a callable is needed, such asarray_map('is_null', $list).- A function with no return value, or one declared
void, producesnullwhen you use its result. - An unset variable behaves like null, but reading it first raises a warning, as described above.
- Objects, closures and resources are never null, even if they look empty.
- Filtering with
array_filter($items, fn($v) => !is_null($v))removes only null entries. Callingarray_filter()with no callback would also remove0,"","0",falseand empty arrays.
Common mistakes
- Confusing null with empty.
empty()and plain truthiness treat0,"0","",falseand[]as empty.is_null()does not. Pick the check that matches your intent. - Using it on possibly undefined variables. You get a warning even though the answer is
true. Initialise variables or useisset()or??. - Expecting isset() and is_null() to be opposites. For a defined variable,
isset($x)is the inverse ofis_null($x). For an undefined variable,isset()is silent whileis_null()warns. - Treating the string "null" as null. Data coming from forms or text formats may contain the word as a string. Convert it deliberately if you need to.
- Forgetting that null can be a legitimate stored value. When you need to know whether a key exists at all, use
array_key_exists().
Related functions
isset()checks that a variable is declared and not null.array_key_exists()checks for the presence of a key regardless of its value.is_bool(),is_int(),is_float(),is_string(),is_array()andis_object()test for other types.is_numeric()checks for numbers and numeric strings.
Recap
is_null(mixed $value): bool returns true only for the null value and false for everything else, including 0, "", false and []. It is equivalent to a strict comparison with null. Avoid passing undefined variables, and choose isset(), empty() or array_key_exists() when your question is about existence or emptiness instead of null.
Tested examples
$values = [null, 0, "", false, [], "null"];
foreach ($values as $value) {
echo json_encode($value), " => ", var_export(is_null($value), true), "\n";
}null => true 0 => false "" => false false => false [] => false "null" => false
$data = ['name' => 'Ada', 'middle' => null];
echo json_encode([
'middle_is_null' => is_null($data['middle']),
'middle_isset' => isset($data['middle']),
'missing_isset' => isset($data['missing']),
'key_exists' => array_key_exists('middle', $data),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"middle_is_null":true,"middle_isset":false,"missing_isset":false,"key_exists":true}
function findName(array $names, int $id): ?string
{
return $names[$id] ?? null;
}
$names = [1 => 'Ada', 2 => 'Grace'];
foreach ([1, 3] as $id) {
$name = findName($names, $id);
echo is_null($name) ? "ID $id: not found" : "ID $id: $name", "\n";
}ID 1: Ada ID 3: not found