How to check whether a variable is null in PHP

Topic: PHP / MySQL« Prev|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.

  • <?php
  • $var = NULL;
  •  
  • // Testing the variables
  • if(is_null($var)){
  •     echo 'This line is printed, because the $var is null.';
  • }
  • echo "<br>";
  •  
  • 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:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.