There are many ways to check Null values in php. This a mandatory requirement for php developers. Here is one perfect example I came a cross a little while ago. That is, when we use session variables, before using it with php code we must check for null values. If not, your php logic will fail. php offers several functions to check the variable contents. Some of the functions are as follows,

php empty(<variable>) function — Determine if a variable is set and is not NULL.

php is_null(<variable>) function — Finds whether a variable is NULL.

You can do a small php script to check how these php functions work with null values. Let’s look at the below php script. I’m declaring a php variable and not assigning any value to it, so that the variable will be a php Null variable. Then in the next steps I’m validate the out come of each php function. Both php functions return true in this scenario.

<?php
 $var1;
    echo(is_null($var1)); //will return 1 that's true
    echo(empty($var1)); // will return 1 that's true
 ?>

Let me show you a real world php programming scenario. Most of the php developers use session to store various data. Most common among those would be user credentials. Let’s assume that you are storing user name in the session when user logs in to the system. How do you do it with php ? The answer is, you assign a session variable for user name like below in php,

$_SESSION['username']='<user name that login>';

Once this is being done in any php page you can call this session variable and validate it. One common usage would be, to check there is  a valid user logged in when they try to access restricted areas in php web pages. We are storing only valid users  in the session for later use in php.  So how do we validate logged in users with php? You would write a conditional statement block in php like shown below;

if($_SESSION['username']!=$_GET['email']){
 //your logic will be here....
 }

Don’t worry about $_GET[’email’]. In php $_GET[] function is used to read values when the html form is submitted to the server. In this php script block user name is the email of current user. So we do the session variable comparison with php the email address submitted by the html form. This php script block will work as far as user navigate with the provided links. But what if a smart user or a hacker try to access your web page directly? This php script will fail because both variables are null. It will not go into your logic. This is the time that php developer should be smarter. You need to check for null values using php, then the problem will be solved. To check null values in your php script can use either of the php functions shown above code. You can alter your php script like below,

//null value check
if(is_null($_SESSION['username'])==true){
 //your logic will be here....
 } 

//session variable check
if($_SESSION['username']!=$_GET['email']){
 //your logic will be here....
 }

Mostly php developers do, if php developer don’t find the matching user using php script we will divert the user to log-in page. If the developer don’t do the null value check with php logic it will not direct the user to the log-in page. With the null value check with php, you can direct users for log-in page if they are not logged in.

This is a very simple and a common mistake done by php developers.  Hope this content would help you. Happy coding.