PHP is_null() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, is_null() checks whether a variable is exactly null. It returns true only for the null value and false for everything else, including 0, an empty string and false.

null means a variable has no value. Functions often return null when something is missing, and database fields can be null too. is_null() lets you test for that without mixing it up with other empty-looking values.

Syntax

Call is_null() with the values you need. Start with this line.

PHP
$missing = is_null($value);
CallWhat it does
is_null(null)Returns true because the value is null.
is_null(0)Returns false because 0 is an integer, not null.
is_null('')Returns false because an empty string is still a string.

is_null($x) gives the same result as $x === null. If the variable was never defined, PHP 8 shows a warning when you pass it to is_null(). Use isset() when a variable might not exist.

Library declarations
PHP
is_null(mixed $value): bool

Parameters

ParameterDescription
valueRequired. The value or variable you want to check.

Return Value

Return ValueDescription
boolReturns true if the value is null, and false otherwise.

PHP Examples

Example 1 — Null Versus Other Values

Check null, zero and an empty string to see that only null counts as null.

PHP
<?php
$a = null;
$b = 0;
$c = '';

var_dump(is_null($a));
var_dump(is_null($b));
var_dump(is_null($c));

How It Works

  1. $a holds null. It has no value at all, so is_null() returns true.
  2. $b holds 0. Zero is an integer value, so the result is false.
  3. $c holds an empty string. It is a string with no characters, which is not null.
  4. Only the first check is true. is_null() never treats empty-looking values as null.

Example 2 — Null Inside an Array

A key can exist and still hold null. Compare is_null(), isset() and array_key_exists() on the same array.

PHP
<?php
$data = ['name' => 'Asha', 'phone' => null];

var_dump(is_null($data['phone']));
var_dump(isset($data['phone']));
var_dump(array_key_exists('phone', $data));
var_dump(is_null($data['name']));

How It Works

  1. The phone key holds null. is_null() looks at the value, so it returns true.
  2. isset() treats null as not set. It returns false for a key whose value is null.
  3. array_key_exists() only checks the key. The key phone exists, so it returns true.
  4. The name key holds a string. is_null() returns false because the value is not null.

Example 3 — Handling a Missing Result

A function returns null when a user is not found. Use is_null() to decide what message to show.

PHP
<?php
function findUser($id) {
    $users = [1 => 'Asha', 2 => 'Ravi'];
    return $users[$id] ?? null;
}

foreach ([3, 1] as $id) {
    $user = findUser($id);

    if (is_null($user)) {
        echo 'User not found', PHP_EOL;
    } else {
        echo 'Hello ', $user, PHP_EOL;
    }
}

How It Works

  1. findUser() looks up an id. The ?? operator returns null if the key does not exist.
  2. The loop tries id 3 first. There is no user 3, so the function returns null.
  3. is_null() catches that case. It is true, so the script prints User not found.
  4. Id 1 exists. The function returns Asha, is_null() is false, and the greeting is printed.

Frequently Asked Questions

It checks whether a value is exactly null. It returns true for null and false for every other value, including 0, false, an empty string and an empty array.
isset() returns false for both undefined variables and variables set to null, and it never raises a warning. is_null() only checks the value, and it raises a warning in PHP 8 if the variable does not exist.
Yes, they give the same result. The === null form is shorter and is very common, while is_null() reads more clearly for beginners and works with functions like array_filter().
No. An empty string is a string value, so is_null('') returns false. Use the empty() construct or a length check if you also want to catch empty strings.
You can, but PHP 8 shows an 'Undefined variable' warning and treats it as null. It is cleaner to use isset() or the ?? operator when a variable may not exist.

Next: is_numeric()

is_numeric() checks whether a value is a number or a numeric string. It returns true for i.

is_numeric() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful