Types7 min readExamples run on PHP 8.5.10

is_null()

Learn how PHP 8.4 is_null() tests whether a value is exactly null, how it differs from isset() and empty checks, and which mistakes to avoid.

Signature

is_null(mixed $value): bool
Available since PHP 4.0.47.48.08.18.28.38.48.5All versions →

Purpose

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

  • $value is 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.

Example
<?php
$values = [null, 0, "", false, [], "null"];
foreach ($values as $value) {
    echo json_encode($value), " => ", var_export(is_null($value), true), "\n";
}
Output
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.

Example
<?php
$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";
Output
{"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.

Example
<?php
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";
}
Output
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, while is_null() is handy where a callable is needed, such as array_map('is_null', $list).
  • A function with no return value, or one declared void, produces null when 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. Calling array_filter() with no callback would also remove 0, "", "0", false and empty arrays.

Common mistakes

  1. Confusing null with empty. empty() and plain truthiness treat 0, "0", "", false and [] as empty. is_null() does not. Pick the check that matches your intent.
  2. Using it on possibly undefined variables. You get a warning even though the answer is true. Initialise variables or use isset() or ??.
  3. Expecting isset() and is_null() to be opposites. For a defined variable, isset($x) is the inverse of is_null($x). For an undefined variable, isset() is silent while is_null() warns.
  4. 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.
  5. Forgetting that null can be a legitimate stored value. When you need to know whether a key exists at all, use array_key_exists().
  • 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() and is_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

Which values are null
<?php
$values = [null, 0, "", false, [], "null"];
foreach ($values as $value) {
    echo json_encode($value), " => ", var_export(is_null($value), true), "\n";
}
Output
null => true
0 => false
"" => false
false => false
[] => false
"null" => false
is_null() compared with isset() and array_key_exists()
<?php
$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";
Output
{"middle_is_null":true,"middle_isset":false,"missing_isset":false,"key_exists":true}
Handling a nullable return value
<?php
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";
}
Output
ID 1: Ada
ID 3: not found