is_null

Checks whether a variable holds null, returning true only for the null value itself.

Signature
is_null(mixed $value): bool
  • Added in PHP 4.0.4

Parameters

ParameterTypeDescription
$valuemixedThe variable being evaluated.

Return value

booltrue if value is null, false otherwise.

is_null() returns true for exactly one value: null. Empty strings, 0, false and empty arrays all return false. It is identical in behaviour to $value === null, and the === form is faster because it avoids a function call — most style guides and static analysers will nudge you toward it.

When to reach for it

The place is_null() earns its keep is as a callback. You can pass its name to array_filter(), array_map() or usort() where an operator will not fit:

$missing = array_filter($row, 'is_null');

It also reads well in a chain of type checks alongside is_string(), is_int() and friends, keeping the style consistent.

What trips people up

Passing an undefined variable emits a warning before is_null() ever runs — the function checks a value, and PHP has to evaluate that value first. is_null($neverSet) gives you Warning: Undefined variable and then true. Use isset() when the variable might not exist at all; it is a language construct and does not evaluate its argument the same way.

The same applies to array keys: is_null($a['missing']) warns, isset($a['missing']) does not.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Filtering out unset columns from a database row with array_filter($row, 'is_null')
  • Distinguishing a stored null from a missing value alongside array_key_exists()
  • Validating that an optional constructor argument was actually omitted

Frequently asked questions

What is the difference between is_null and isset?

is_null() returns true when a value is null, while isset() returns false for both null values and variables that do not exist. isset() also suppresses the undefined-variable warning that is_null() triggers.

Is is_null the same as === null?

Yes, the results are identical. $value === null is marginally faster because it avoids a function call, so prefer it except when you need is_null as a callback name.

Why does is_null return true but throw a warning?

Because the variable or array key does not exist. PHP evaluates the argument first, emits "Undefined variable" or "Undefined array key", substitutes null, and is_null() then reports true. Switch to isset() to check existence.

More examples

Using is_null as an array_filter callback

array_filter() keeps entries whose callback returns true, so $missing holds only the null columns. The second call inverts the test to get the populated fields, keys preserved.

Example · php
<?php
$row = ['id' => 3, 'name' => 'Ada', 'email' => null, 'phone' => null];

$missing = array_filter($row, 'is_null');
$present = array_filter($row, fn($v) => !is_null($v));

echo "Missing columns: ", implode(', ', array_keys($missing)), "\n";
print_r($present);

Null versus missing key

isset() cannot tell a stored null from an absent key — both give false. array_key_exists() proves the key is there, and is_null() confirms its value. The ?? operator treats null and missing identically, which is why the default wins here.

Example · php
<?php
$config = ['timeout' => null];

var_dump(isset($config['timeout']));
var_dump(array_key_exists('timeout', $config));
var_dump(is_null($config['timeout']));
var_dump(array_key_exists('retries', $config));

$timeout = $config['timeout'] ?? 30;
echo "timeout = $timeout\n";

Discussion

  • Be the first to comment on this lesson.