How to check whether a variable is null in PHP
Topic: PHP / MySQLPrev|Next
Answer: Use the PHP is_null()
function
You can use the PHP is_null()
function to check whether a variable is null or not.
Let's check out an example to understand how this function works:
Example
Try this code »<?php
$var = NULL;
// Testing the variable
if(is_null($var)){
echo 'This line is printed, because the $var is null.';
}
?>
The is_null()
function also returns true
for undefined variable. Here's a example:
Example
Try this code »<?php
// Testing an undefined variable
if(is_null($inexistent)){
echo 'This line is printed, because the $inexistent is null.';
}
?>
Related FAQ
Here are some more FAQ related to this topic: