Summary: in this tutorial, you’ll learn how to use the PHP is_null() construct to check if a variable is null.
Introduction to the PHP is_null() construct #
PHP is_null() accepts a variable and returns true if that variable is null. Otherwise, it returns false.
is_null(mixed $v): boolCode language: PHP (php)In this syntax, the $v is the variable to check. If $v doesn’t exist, the is_null() also returns true and issues a notice.
Since the is_null() is a language construct, not a function, you cannot call it via variable functions. For example, the following statement will result in an error:
<?php
$f = is_null;Code language: PHP (php)However, you can define a function that wraps the is_null() construct like this:
<?php
function isnull($v): bool
{
return is_null($v);
}Code language: PHP (php)Alternatively, you can define an arrow function, assign it to a variable, and use that variable function.
<?php
$isnull = fn($v) => is_null($v);
$color = null;
echo $isnull($color); // trueCode language: PHP (php)PHP is_null() examples #
The following example uses the is_null() construct and returns true because the $count variable doesn’t exist:
<?php
var_dump(is_null($count));Code language: PHP (php)This code also issues a notice:
Notice: Undefined variable: $countCode language: PHP (php)The following example uses the is_null() and returns true because the $count variable is null:
<?php
$count = null;
var_dump(is_null($count)); // trueCode language: PHP (php)The following example uses the is_null() and returns false because the $count variable is not null:
<?php
$count = 1;
var_dump(is_null($count)); // falseCode language: PHP (php)PHP is_null() with array #
The following example uses the is_null() to check if the element with the key link is null or not. It returns true because the element doesn’t exist:
<?php
$colors = [
'text' => 'black',
'background' => 'white'
];
var_dump(is_null($colors['link']));Code language: PHP (php)It also returns a notice:
Notice: Undefined index: linkCode language: PHP (php)PHP is_null() with string index #
The following example uses the is_null() to check if the element at index 5 in the string $message is null or not:
<?php
$message = 'Hello';
var_dump(is_null($message[5]));Code language: PHP (php)It returns false and issues a notice:
PHP Notice: Uninitialized string offset: 5Code language: PHP (php)PHP is_null(), equal operator (==), and identity operator (===) #
The echo displays an empty string for the false value, which is not intuitive. The following defines a function that displays false as the string false instead of an empty string:
<?php
function echo_bool(string $title, bool $v): void
{
echo $title, "\t", $v === true ? 'true' : 'false', PHP_EOL;
}Code language: PHP (php)Comparing falsy values with null using equal operator (==) #
Comparing a falsy value with null using the equal operator (==) will return true. For example:
The following example compares null with falsy values using the equal operator (==):
<?php
function echo_bool(string $title, bool $v): void
{
echo $title, "\t", $v === true ? 'true' : 'false', PHP_EOL;
}
echo_bool('null == false:', null == false);
echo_bool('null == 0:', null == 0);
echo_bool('null == 0.0:', null == 0.0);
echo_bool('null =="0":', null == false);
echo_bool('null == "":', null == '');
echo_bool('null == []:', null == []);
echo_bool('null == null:', null == null);Code language: PHP (php)Output:
null == false: true
null == 0: true
null == 0.0: true
null =="0": true
null == "": true
null == []: true
null == null: trueCode language: plaintext (plaintext)Comparing falsy value with null using identity operator (===) #
The following example uses the identity operator (===) to compare null with falsy values, only null === null returns true.
<?php
function echo_bool(string $title, bool $v): void
{
echo $title, "\t", $v === true ? 'true' : 'false', PHP_EOL;
}
echo_bool('null === false:', null === false);
echo_bool('null === 0:', null === 0);
echo_bool('null === 0.0:', null === 0.0);
echo_bool('null ==="0":', null === false);
echo_bool('null === "":', null === '');
echo_bool('null === []:', null === []);
echo_bool('null === null:', null === null);Code language: plaintext (plaintext)Output:
null === false: false
null === 0: false
null === 0.0: false
null ==="0": false
null === "": false
null === []: false
null === null: trueCode language: PHP (php)Comparing falsy values with null using the PHP is_null() #
The following example uses the is_null() to check if falsy values are null:
<?php
function echo_bool(string $title, bool $v): void
{
echo $title, "\t", $v === true ? 'true' : 'false', PHP_EOL;
}
echo_bool('is_null(false):', is_null(false));
echo_bool('is_null(0):', is_null(0));
echo_bool('is_null(0.0)', is_null(0.0));
echo_bool('is_null("0"):', is_null("0"));
echo_bool('is_null(""):', is_null(""));
echo_bool('is_null([]):', is_null([]));
echo_bool('is_null(null):', is_null(null));Code language: PHP (php)Output:
is_null(false): false
is_null(0): false
is_null(0.0) false
is_null("0"): false
is_null(""): false
is_null([]): false
is_null(null): trueCode language: PHP (php)The is_null() and identity operator (===) return the same result.
Summary #
- The
is_null()checks a value and returnstrueif that value isnull. Otherwise, it returnsfalse. - The
is_null()behaves the same as the identity operator (===).