Monday, June 15, 2026
HomeLanguagesHow to check whether a variable is null in PHP ?

How to check whether a variable is null in PHP ?

We have given a variable and the task is to check whether the value of given variable is null or not and returns a Boolean value using PHP. To check a variable is null or not, we use is_null() function. A variable is considered to be NULL if it does not store any value. It returns TRUE if value of variable $var is NULL, otherwise, returns FALSE.
 

Syntax

boolean is_null( $var )

Example:

PHP




<?php
// PHP Program to check whether
// a variable is null or not?
 
$var1 = NULL;
$var2 = "\0"; // "\0" means null character
$var3 = "NULL";
 
// $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");
 
?>


Output:

True
False
False
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS