Okay, so it's time to learn about the special data type of null in PHP.
That's very interesting data type, because it means that something is empty or it actually means nothing,
and nothing can be associated to a variable and you can compare something to nothing.
So let's see more examples, because in this case, we really need to see all the examples to understand
the usefulness of the null data type.
Let's start by creating a null PHP file and adding a PHP tag.
Now let's start with var dumping a couple of things.
So we're gonna compare null to different data types to values that look empty.
First let's compare and keep in mind I'm using the comparison operator.
Not strict comparison operator.
So it ignores the data types.
So first let's compare null to null.
Then let's see if null is anything similar to false.
Then let's compare null to zero.
Then let's compare null to empty string.
Next up, let's compare it to empty array.
Um that was assignment.
Let me fix that.
Next up what else can we do.
Well maybe that's enough.
Now let's run PHP null PHP and we've got well basically five times.
True.
So the conclusion here is that PHP thinks that null or false or zero or empty string or empty array.
This is nothing.
This is empty.
Okay.
Now let's see another example.
Let me add a non-existing variable and let's see what PHP will return for that.
So other than warning we see that it has returned null.
So if the variable doesn't exist, PHP returns nothing for it, which is null essentially.
So null is also used to tell you that there is nothing to be returned, or that something contains nothing.
This is what null means.
Now it's also fine to just directly assign null to a variable or pass it around.
If we run that again, the warning is gone, but the variable still contains the value null.
Now next up in PHP you can check if a certain variable is defined.
So to the var dump list let me add the function call.
This function is called is set.
And then you just pass a variable that you want to verify.
Maybe DCA we know it doesn't exist.
So let's run the script now.
Okay so it returned false.
It doesn't exist.
We can see that we haven't defined this variable.
So what happens if I check for the ABC variable that contains null?
Let's run that.
Hmm.
So maybe this isn't super intuitive, but this is just one of the things in PHP that you would have
to remember.
So that's another I would say special case.
I can say that this variable exists, but since its value is null is set returns false.
Now you can also check a value for explicitly bank null.
The reason is null function.
And if I run ABC, I also get true because well this indeed is null.
Next up, sometimes when there is a possibility that something is null, you might want to have an alternative
or a default value.
So let me have this DB variable And I'd like to assign ABC, but ABC can be null and we actually see
that it's null.
So then we can use a null coalescing operator to question marks and provide a default.
So the way it works is the ABC value would be assigned to DB variable.
If ABC is not null.
If it's null, then the value on the right which is default.
Let's output that.
Then we have db.
Now let's run the script again and it contains the default value because well this is how the null coalescing
operator works.
Next up let's see an example with a function parameter.
So I will have a grid function that we've used before.
And I expect the name to be a string.
What if someone would not provide a value for the name?
Well currently that's not possible, especially if we would do declare strict types.
Equals one which requires us to respect the types strictly.
So let me implement this function.
So I would like to say hello and then concatenate that with the name.
And maybe with an exclamation mark.
And currently you have to pass something to it.
So you have to pass let's say a lease.
And it's not possible to not pass anything or pass null.
So if I run it now, there will be a fatal error.
So you can make parameters nullable if they are typed by adding a question mark before the type.
So now you still have to provide the parameter value but it can be null.
So this just produces text like this which is not ideal.
Maybe let's add a new line here.
So it is clearly visible.
We just got an empty placeholder here.
And again for such cases we can use the null coalescing operator and add the default, which would be
stranger, which would be displayed if the name is not provided.
Now by the way, since I've explained this exit function, another interesting PHP function that you
actually might use often is empty.
So empty will just return true for all these cases.
If something is null, if it's false, if it's zero, if it is an empty string or an empty array.
So let's just try one example.
You can try more on your own, But that's a function to remember.
So I think um, that's this one.
So obviously empty array is empty.
So that's a function to remember.
This one is a function to remember is set and also is null is a handy function Rthy.
Now another example is how can you remove the elements from the array that are equal to null.
So this includes null itself false zero empty string empty array.
So it's pretty important to also understand what would equal null because this is used across PHP language
for different cases like the one you're going to see right now.
So there is a function called array filter.
It works with arrays.
Now you can provide an anonymous function to it to process individual array elements the way you like,
but by default it will just compare every single value to null, and if it is null or.
It equals null when it is not strictly compared compared.
So ignoring the type like an empty string or empty array.
And then let's add a real number.
It will remove all those elements from the resulting array.
So if I run this right now you see that we only got two elements left.
That's this number and that number.
Everything else is gone.
So that's also something I will repeat again that you should remember about those equalities.
That null is also false.
And if you compare those values this will return true.
So there is always something more.
And I can also tell you a little bit about null with objects.
But we haven't talked about objects yet so I think that's enough for now.