Q:

How to check whether a variable is null in PHP

0

How to check whether a variable is null in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this 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:

<?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:

<?php
// Testing an undefined variable
if(is_null($inexistent)){
    echo 'This line is printed, because the $inexistent is null.';
}
?>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to reverse a string in PHP... >>
<< How to check whether a variable is empty in PHP...