PHP | is_null() Function

The is_null() function is an inbuilt function in PHP which is used to find whether a variable is NULL or not.

Syntax:

 boolean is_null ( $var )

Parameters: This function accepts a single parameter as shown in above syntax and described below.



  • $var: Variable to check if it is NULL or not.

Return value: It returns a boolean value. That is, it returns TRUE when $var will be NULL, otherwise it returns FALSE.

Below programs illustrate the is_null() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to demonstrate the working of is_null() function
$var1 = NULL;
$var2 = "\0"; // "\0" means "\0"
$var3 = "NULL";
$var4 = 0;
  
// $var1 has NULL value, so always give TRUE
is_null($var1) ? print_r("True\n") : print_r("False\n");
  
// $var2 has '\0' value which consider as null in 
// c and c++ but here taken as string, gives FALSE
is_null($var2) ? print_r("True\n") : print_r("False\n");
  
// $var3 has NULL string value so it will false
is_null($var3) ? print_r("True\n") : print_r("False\n");
  
// $var4 is 0, gives FALSE
is_null($var4) ? print_r("True\n") : print_r("False\n");
  
?>

chevron_right


Output:

True
False
False
False

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to demonstrate the working of
// is_null() function
  
function check_null($var)
{
    return (is_null($var) ? "True" : "Flase");
}
  
echo check_null(NULL) . "\n"
echo check_null(null) . "\n"
echo check_null(Null) . "\n"
echo check_null(NUll) . "\n"
echo check_null(NULl) . "\n"
echo check_null(nulL) . "\n"
echo check_null(nuLL) . "\n"
echo check_null(nULL) . "\n"
  
echo check_null(Nul) . "\n"
echo check_null(false) . "\n"
  
?>

chevron_right


Output:

True
True
True
True
True
True
True
True
Flase
Flase

Reference : http://php.net/manual/en/function.is-null.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.