Trusted answers to developer questions

What Is the is_null method in PHP?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

The is_null method can be used to check if a variable is NULL or not.

Syntax

is_null(mixed $value): bool

The is_null method returns true if the passed value is NULL. Otherwise, it returns false.

Code

<?php
printIsNull($test); // undefined variable is NULL
$test = NULL;
printIsNull($test);
$test = 0;
printIsNull($test);
$test = 'NULL';
printIsNull($test);
function printIsNull($test){
echo "The Value is: ";
var_dump($test);
echo "Is null: ";
var_dump(is_null($test));
echo "---------\n\n";
}
?>

Explanation

In the code above:

  • We use the is_null method to check if a variable is NULL or not.

  • For the undeclared variable and for the value NULL, the is_null method returns true.

  • But for the values 0(int) and 'NULL'(string), the is_null method returns false because they are not NULL values.

RELATED TAGS

php
Did you find this helpful?