What Is the is_null method in PHP?

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.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources