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.
10 Comments
Join the discussion and tell us your opinion.
Beneficial info and excellent design you got here! I want to thank you for sharing your ideas and putting the time into the stuff you publish! Great work!
Check for === and !== operators, they claim it is faster than is_null()
http://www.php.net/manual/en/language.types.null.php#77937
Yes rasika…thanks for pointing it out..
This really answered my downside, thank you!
What a great resource!
Hey, I am checking this blog using the phone and this appears to be kind of odd. Thought you’d wish to know. This is a great write-up nevertheless, did not mess that up.
– David
Wow! Thank you! I constantly wanted to create in my web site something like that. Can i get part of your
post to my weblog?
I pretty much agree with all your thoughts you said in your post, especially at the end of your article. Thank you, this info is very useful as always. Keep up the good work! You’ve got +1 more reader of your super blog:) Isabella S.
sryforthatall
I think youve made some truly interesting points. Not too many people would actually think about this the way you just did. Im really impressed that theres so much about this subject thats been uncovered and you did it so well, with so much class. Good one you, man! Really great stuff here.