WEBVTT

00:00.560 --> 00:05.900
Okay, so it's time to learn about the special data type of null in PHP.

00:05.930 --> 00:14.810
That's very interesting data type, because it means that something is empty or it actually means nothing,

00:14.810 --> 00:22.970
and nothing can be associated to a variable and you can compare something to nothing.

00:23.000 --> 00:31.010
So let's see more examples, because in this case, we really need to see all the examples to understand

00:31.010 --> 00:34.100
the usefulness of the null data type.

00:34.130 --> 00:40.190
Let's start by creating a null PHP file and adding a PHP tag.

00:40.220 --> 00:45.260
Now let's start with var dumping a couple of things.

00:45.650 --> 00:51.710
So we're gonna compare null to different data types to values that look empty.

00:52.130 --> 00:58.460
First let's compare and keep in mind I'm using the comparison operator.

00:58.490 --> 01:01.100
Not strict comparison operator.

01:01.100 --> 01:03.560
So it ignores the data types.

01:03.560 --> 01:06.270
So first let's compare null to null.

01:06.300 --> 01:11.790
Then let's see if null is anything similar to false.

01:11.790 --> 01:15.210
Then let's compare null to zero.

01:15.810 --> 01:19.440
Then let's compare null to empty string.

01:19.740 --> 01:23.370
Next up, let's compare it to empty array.

01:23.460 --> 01:25.620
Um that was assignment.

01:25.650 --> 01:26.550
Let me fix that.

01:26.580 --> 01:28.890
Next up what else can we do.

01:28.890 --> 01:30.960
Well maybe that's enough.

01:31.590 --> 01:39.000
Now let's run PHP null PHP and we've got well basically five times.

01:39.000 --> 01:39.720
True.

01:39.750 --> 01:49.110
So the conclusion here is that PHP thinks that null or false or zero or empty string or empty array.

01:49.140 --> 01:50.610
This is nothing.

01:50.610 --> 01:52.650
This is empty.

01:52.920 --> 01:53.400
Okay.

01:53.400 --> 01:55.590
Now let's see another example.

01:55.590 --> 02:01.980
Let me add a non-existing variable and let's see what PHP will return for that.

02:01.980 --> 02:07.410
So other than warning we see that it has returned null.

02:07.410 --> 02:15.210
So if the variable doesn't exist, PHP returns nothing for it, which is null essentially.

02:15.210 --> 02:24.990
So null is also used to tell you that there is nothing to be returned, or that something contains nothing.

02:25.020 --> 02:27.390
This is what null means.

02:28.770 --> 02:36.060
Now it's also fine to just directly assign null to a variable or pass it around.

02:36.090 --> 02:43.050
If we run that again, the warning is gone, but the variable still contains the value null.

02:45.180 --> 02:50.370
Now next up in PHP you can check if a certain variable is defined.

02:50.400 --> 02:54.870
So to the var dump list let me add the function call.

02:54.870 --> 02:56.610
This function is called is set.

02:56.610 --> 02:59.910
And then you just pass a variable that you want to verify.

02:59.940 --> 03:03.990
Maybe DCA we know it doesn't exist.

03:03.990 --> 03:06.900
So let's run the script now.

03:08.190 --> 03:09.990
Okay so it returned false.

03:09.990 --> 03:10.800
It doesn't exist.

03:10.830 --> 03:13.600
We can see that we haven't defined this variable.

03:13.630 --> 03:19.780
So what happens if I check for the ABC variable that contains null?

03:20.140 --> 03:21.280
Let's run that.

03:22.090 --> 03:22.600
Hmm.

03:22.990 --> 03:30.910
So maybe this isn't super intuitive, but this is just one of the things in PHP that you would have

03:30.910 --> 03:31.990
to remember.

03:32.260 --> 03:35.260
So that's another I would say special case.

03:35.290 --> 03:42.550
I can say that this variable exists, but since its value is null is set returns false.

03:44.590 --> 03:48.670
Now you can also check a value for explicitly bank null.

03:48.700 --> 03:51.940
The reason is null function.

03:53.050 --> 03:59.710
And if I run ABC, I also get true because well this indeed is null.

04:01.180 --> 04:09.370
Next up, sometimes when there is a possibility that something is null, you might want to have an alternative

04:09.370 --> 04:11.140
or a default value.

04:13.420 --> 04:21.250
So let me have this DB variable And I'd like to assign ABC, but ABC can be null and we actually see

04:21.250 --> 04:22.480
that it's null.

04:22.480 --> 04:31.720
So then we can use a null coalescing operator to question marks and provide a default.

04:34.240 --> 04:39.940
So the way it works is the ABC value would be assigned to DB variable.

04:39.940 --> 04:41.590
If ABC is not null.

04:41.590 --> 04:45.190
If it's null, then the value on the right which is default.

04:45.220 --> 04:46.840
Let's output that.

04:47.620 --> 04:49.540
Then we have db.

04:50.440 --> 04:57.670
Now let's run the script again and it contains the default value because well this is how the null coalescing

04:57.670 --> 04:58.840
operator works.

05:00.640 --> 05:04.540
Next up let's see an example with a function parameter.

05:05.440 --> 05:09.340
So I will have a grid function that we've used before.

05:09.340 --> 05:12.760
And I expect the name to be a string.

05:12.790 --> 05:17.110
What if someone would not provide a value for the name?

05:17.110 --> 05:25.670
Well currently that's not possible, especially if we would do declare strict types.

05:28.040 --> 05:33.710
Equals one which requires us to respect the types strictly.

05:35.180 --> 05:37.100
So let me implement this function.

05:37.100 --> 05:45.590
So I would like to say hello and then concatenate that with the name.

05:48.110 --> 05:50.450
And maybe with an exclamation mark.

05:50.480 --> 05:54.830
And currently you have to pass something to it.

05:55.160 --> 05:57.860
So you have to pass let's say a lease.

05:57.860 --> 06:04.220
And it's not possible to not pass anything or pass null.

06:04.220 --> 06:09.410
So if I run it now, there will be a fatal error.

06:09.740 --> 06:17.060
So you can make parameters nullable if they are typed by adding a question mark before the type.

06:18.050 --> 06:23.930
So now you still have to provide the parameter value but it can be null.

06:29.330 --> 06:36.050
So this just produces text like this which is not ideal.

06:36.080 --> 06:38.120
Maybe let's add a new line here.

06:38.120 --> 06:40.670
So it is clearly visible.

06:40.850 --> 06:44.150
We just got an empty placeholder here.

06:44.150 --> 06:52.370
And again for such cases we can use the null coalescing operator and add the default, which would be

06:52.370 --> 06:57.380
stranger, which would be displayed if the name is not provided.

06:59.690 --> 07:06.470
Now by the way, since I've explained this exit function, another interesting PHP function that you

07:06.470 --> 07:09.560
actually might use often is empty.

07:09.590 --> 07:14.000
So empty will just return true for all these cases.

07:14.000 --> 07:22.250
If something is null, if it's false, if it's zero, if it is an empty string or an empty array.

07:22.700 --> 07:24.860
So let's just try one example.

07:24.860 --> 07:30.720
You can try more on your own, But that's a function to remember.

07:30.720 --> 07:33.570
So I think um, that's this one.

07:33.570 --> 07:36.720
So obviously empty array is empty.

07:37.320 --> 07:39.330
So that's a function to remember.

07:39.330 --> 07:46.770
This one is a function to remember is set and also is null is a handy function Rthy.

07:47.310 --> 07:57.810
Now another example is how can you remove the elements from the array that are equal to null.

07:57.840 --> 08:03.450
So this includes null itself false zero empty string empty array.

08:04.440 --> 08:13.560
So it's pretty important to also understand what would equal null because this is used across PHP language

08:13.560 --> 08:16.890
for different cases like the one you're going to see right now.

08:16.890 --> 08:20.220
So there is a function called array filter.

08:20.220 --> 08:21.780
It works with arrays.

08:21.810 --> 08:29.670
Now you can provide an anonymous function to it to process individual array elements the way you like,

08:29.670 --> 08:37.650
but by default it will just compare every single value to null, and if it is null or.

08:37.680 --> 08:41.760
It equals null when it is not strictly compared compared.

08:41.760 --> 08:47.100
So ignoring the type like an empty string or empty array.

08:47.100 --> 08:49.500
And then let's add a real number.

08:49.500 --> 08:53.700
It will remove all those elements from the resulting array.

08:53.700 --> 09:00.210
So if I run this right now you see that we only got two elements left.

09:00.240 --> 09:02.790
That's this number and that number.

09:02.790 --> 09:04.680
Everything else is gone.

09:04.680 --> 09:14.820
So that's also something I will repeat again that you should remember about those equalities.

09:15.360 --> 09:17.130
That null is also false.

09:17.130 --> 09:21.000
And if you compare those values this will return true.

09:22.680 --> 09:24.870
So there is always something more.

09:24.870 --> 09:30.030
And I can also tell you a little bit about null with objects.

09:30.030 --> 09:35.250
But we haven't talked about objects yet so I think that's enough for now.
