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:
PHP
<?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" ); ?> |
Output:
True False False False
Program 2:
PHP
<?php // PHP code to demonstrate the working of // is_null() function function check_null( $var ) { return ( is_null ( $var ) ? "True" : "False" ); } 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" ; ?> |
Output:
True True True True True True True True False False