Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this video, we're going to be talking about NULL. NULL is a fancy term for nothing, for not having a value. It's not zero. It's not an empty string. It's the actual lack of a value. And if we can set a value into a variable, we also have to have some way to talk about the fact that variable might not have a value at all. And null allows us to do that. Let's take a look at it by first creating a page where we can work with null. And I'm going to do that by opening up basic.html, and then choosing Save As. We're going to save this as null.php. So, null now, like true and false, the booleans that we just talked about. Null can either be written in lower case, or in all upper case. And you'll waste a lot of time arguing with people about the right way to do it in PHP.
And whether one is better than the other, or one has better performance than the other. The truth is that they're case insensitive and that you can use absolutely whichever one you prefer. There is no difference. I'm going to use the lower case version. And let's set a variable equal to var1 equal to null. And just for contrast, let's set var2 equal to an empty string. Just so that we can see the difference between those two. Now, like a lot of the other types that we've looked at, there are functions that allow us to test whether it is of a certain type. So, we can test whether this is null or not.
is_null will tell us if the variable is null, and we'll do that to both of these. Let's also do it to a third one as well. Make this var3, then we'll test var3. Now, var3 isn't set, right? It hasn't been set to anything, so we'll see what that gives us back. So we return to Firefox, and set up Booleans. We'll load it up null.php. So, it came back and said is variable 1 null? Yes, it is. Variable 2, is that null? No, it's not. That was my empty string. So, that's not considered null. var3, is it null? I get a notice here that's not an error. That's not a warning.
It's a notice just saying hey, level heads up here, you're in the middle of developing. And I noticed that you're making a reference to an undefined variable so I want to call your attention to it here. Note that these are different than, than warnings in PHP. But it goes ahead after that notice and gives us the result, which is the yes in fact, var3 is null. The fact that it hasn't been set means that it has no value. So, the answer is true, it is null. So, notice the difference between that. Notice what happened with var1 and what happened with var3. By setting it specifically to null, I avoid this notice that pops up.
And we can contrast that with what happens if we have is set. Alright, so let me just paste in a few others here. var1 is set, var2 is set, or var3 is set. Is set doesn't have a, a space here, no underscore between is set, it's all run together. And let's just reload the page now. And you can see that it tells us that var1 is not set, var3 is not set, and var2 is set. Now, not set means that it's null, right? So being null, we're being told, is the same thing as being unset. Now, we know it's not exactly the same thing.
We saw that with is_null. But it's close. For the purposes of most of our tests, it will be the same. So that's what null is, null is just the lack of having a value. That's a pretty easy concept. But I think this is also a good spot for us to talk about a function in PHP called Empty. And as you would expect, empty returns a boolean, true or false, for whether a variable is considered to be empty. What you might not expect though, are the things that it considers to be empty. In PHP, empty is going to be an empty string, null, 0, 0.0. Meaning, a float in which the value is zero, a string with a zero inside of it, false, or an empty array.
All of those things are considered empty. So, we can provide our test again here, for whether these things are considered empty. And let's define var3 here, just so it has a value. var3, it's got a dollar sign in front. var3 is going to be equal to, let's make it zero inside the string, like that. Let's go back over to our browser and let's try this out. And let's see what things are empty. Is var1 empty? Yes. var2 empty? Yes. var3 is empty also. So null, an empty string.
And this is a very important one, 0 inside the string are all considered to be empty. Now, this broad, catch all approach can be extremely useful, but you have to use it with a lot of caution. For example, imagine that you had a web form that was submitting a value to your PHP code. And imagine that, that field was, how many children do you have? You want to check if that field is left blank by the user. And if not, you want to say hey, you need to fill out this. I need to know how many children you have. Empty would detect an empty string or no value being submitted, which is what you want.
But it would also not allow the number of children you have to be zero as a value. Which is probably not what you intended, right? They would not be able to submit the value of zero. Just like we saw here, that would be considered empty as well. So most times, you'll need to use further conditions with empty such as, is it empty and also not numeric? Or is it empty but not null? Now, I'm mentioning this now because empty is a leading cause of bugs in PHP code. So, something you're really going to want to keep an eye out for.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98353 Viewers
61 Video lessons · 85643 Viewers
71 Video lessons · 69584 Viewers
56 Video lessons · 101916 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.