WEBVTT

00:00.380 --> 00:00.740
Okay.

00:00.740 --> 00:04.520
So now let's see some data types in PHP in practice.

00:04.520 --> 00:09.890
And the first step is to create a types PHP file and then open it.

00:09.890 --> 00:12.320
And let's see some examples.

00:13.490 --> 00:20.240
So let's start by adding the PHP starting tag and define the is student variable.

00:20.240 --> 00:26.600
But this time let's not associate a boolean value with it, but instead a number.

00:26.600 --> 00:28.910
And also to output data.

00:28.910 --> 00:34.340
We're not going to use the echo statement, but a var dump function.

00:35.780 --> 00:42.470
So because we use vardump, we're not only going to see the value of the variable, but also its type.

00:42.470 --> 00:47.000
Like in this example that's an integer and the value is one.

00:48.830 --> 00:55.550
So next up something that you will often do with programming in general is you will make some conditions.

00:55.550 --> 00:59.870
And depending on those conditions something else would happen.

00:59.870 --> 01:06.940
So you will often check if the value of specific expression is either true or false.

01:06.970 --> 01:15.520
Now, fortunately, with this Vardump function, we can output multiple values by calling it just once

01:15.520 --> 01:17.530
by separating them with a comma.

01:17.560 --> 01:27.070
So now I can compare the student variable using the comparison operator which is two equality signs

01:27.070 --> 01:28.810
with true.

01:29.410 --> 01:31.030
Now let me run that.

01:31.030 --> 01:34.120
And the result of that is true.

01:34.150 --> 01:41.020
Now we see that if student is an integer and we are comparing it with true.

01:41.050 --> 01:49.030
Now if I would also dump the value of true, we can see that this is of type boolean.

01:49.060 --> 01:51.490
So those are different types.

01:51.490 --> 01:55.930
And what happened here is again type coercion.

01:55.930 --> 01:59.260
So PHP is trying to convert the types.

01:59.260 --> 02:06.060
So the whole expression here makes sense so that the values can be compared.

02:06.960 --> 02:14.970
Now you should know that in PHP every number that's non-zero will just evaluate to true.

02:14.970 --> 02:16.950
And this is often used.

02:16.950 --> 02:26.640
So if I for example check with minus one then this comparison is also true.

02:26.670 --> 02:33.780
But if is student is set to zero this is then false.

02:35.880 --> 02:39.210
Okay so let me change the value here to one.

02:40.140 --> 02:49.110
Now here in this comparison let's use a strict equality operator which is not two equal signs but three

02:49.140 --> 02:50.970
equal signs together.

02:50.970 --> 02:52.590
And let's run that.

02:53.670 --> 02:58.410
So as we see the result of this comparison is now false.

02:59.170 --> 03:01.510
And nothing else has changed.

03:01.510 --> 03:11.200
So that's another way of comparing values in PHP that just takes into account also the types of the

03:11.200 --> 03:14.230
two expressions on the left and on the right side.

03:15.430 --> 03:20.050
So with just two equality signs, the type coercion will happen.

03:20.050 --> 03:28.210
But if you use the strict equality operator, then you are also making sure that the two values, the

03:28.210 --> 03:36.520
two expressions are of the same exact type, and only then you verify if the value is identical.

03:37.600 --> 03:40.750
Okay, so next up let's see an example with an array.

03:40.780 --> 03:46.480
Let's define a variable called scores and an array starts with square brackets.

03:46.480 --> 03:48.790
And let's just put some values in there.

03:48.790 --> 03:51.670
This would be number 85 an integer.

03:51.670 --> 03:55.150
But obviously we want to make things complex.

03:55.150 --> 03:58.660
So the next value would be a string 90.

03:59.440 --> 04:06.490
Okay, now let's vardump the result of just summing up all those values.

04:06.910 --> 04:15.340
So the way you access elements in the array is if that's an indexed array and it is an indexed array

04:15.400 --> 04:19.030
I just use the indexes starting from zero.

04:19.630 --> 04:26.680
So I'm gonna just add the scores zero to scores index one.

04:28.150 --> 04:32.920
And as you see the result is 175.

04:32.920 --> 04:39.760
So the types were coerced or this is sometimes also called type juggling.

04:39.760 --> 04:43.570
So the types were coerced to an integer.

04:43.960 --> 04:44.740
Okay.

04:44.740 --> 04:47.770
So let's make it even more interesting.

04:47.770 --> 04:51.940
Let's add another value 95 and half.

04:51.970 --> 04:56.650
That is a float value floating point number.

04:56.860 --> 04:58.160
So let's add this.

04:58.160 --> 05:00.290
The index here would be two.

05:00.320 --> 05:03.500
So as I said we index from zero.

05:03.770 --> 05:06.230
Let's see what will happen now.

05:06.260 --> 05:08.810
So now the result is float.

05:08.810 --> 05:14.120
So as we see there is some precedence in what type is being used.

05:14.360 --> 05:22.850
If I remove this string and then I also need to get rid of this element.

05:22.880 --> 05:24.590
Let's see what will happen.

05:24.590 --> 05:27.890
So we also get the float as a result.

05:27.920 --> 05:36.680
Now what's interesting if I surround this float value in quotes in double quotes.

05:36.680 --> 05:44.300
So this is a string now and I add those two values I still get a float.

05:44.600 --> 05:55.010
So what we see here is just PHP is trying to guess what's the best type that it should be using to calculate

05:55.010 --> 05:58.070
something sensible from an expression.

05:59.150 --> 06:03.320
Now we can also vardump the scores array.

06:03.920 --> 06:06.590
So let's see how this would look like.

06:07.670 --> 06:10.010
So this is how the array would be outputted.

06:10.010 --> 06:14.030
We see that there is the index zero which contains an integer.

06:14.030 --> 06:18.620
And at index one we've got a string.

06:20.450 --> 06:25.940
So you can also influence the type of the value by typecasting.

06:25.970 --> 06:30.170
So in this example here let's have an output.

06:30.170 --> 06:33.590
Again we've got an array that has an integer.

06:33.590 --> 06:39.860
And the string though the result is float because it makes sense according to PHP.

06:39.890 --> 06:44.570
So I can do typecasting by using this int operator.

06:44.570 --> 06:49.400
And now I'm gonna first convert this element to an integer.

06:49.430 --> 06:51.320
Let's see what will happen then.

06:51.320 --> 06:54.860
So now in the array we've got two integers.

06:54.860 --> 07:01.770
This one and that one, so that the result of adding those is also an integer.

07:01.830 --> 07:09.960
Now keep in mind that this way here is just a way to cast to another type that might not be necessarily

07:09.960 --> 07:16.860
the best way to get an integer out of a floating point number, because you might want to apply different

07:16.860 --> 07:17.520
rules.

07:17.520 --> 07:22.590
And we're going to talk about that later on when I will talk more about numbers.

07:22.830 --> 07:25.950
For now, let's also see another example.

07:25.950 --> 07:28.710
This time we're going to use echo.

07:28.710 --> 07:38.130
And maybe here I'm just going to move those scores to the total variable and change it that way.

07:38.130 --> 07:42.300
So now that I can echo the value of total.

07:42.330 --> 07:45.180
So this would be total score is.

07:45.180 --> 07:48.840
And previously we have used the concatenation operator.

07:48.840 --> 07:50.400
So dot.

07:51.420 --> 07:55.410
So if I would like to uh sorry the name is Total.

07:55.410 --> 07:59.940
If I would like to output the score, I can do something like this.

07:59.940 --> 08:02.730
The total score is 180.

08:02.790 --> 08:05.760
I can also add a new line here.

08:08.250 --> 08:09.780
Like this.

08:09.780 --> 08:18.450
But what we can also do, and that only works with the double quotes, is I can just directly put a

08:18.450 --> 08:22.950
variable inside double quotes like this.

08:22.950 --> 08:28.110
As you see, even the code editor is highlighting this as something separate.

08:30.030 --> 08:33.150
And if I run this again, this also works.

08:33.150 --> 08:42.330
Now keep in mind that you can't put variables like that inside single quotes, because this would just

08:42.360 --> 08:44.580
output the name of the variable.

08:44.580 --> 08:49.050
And also special characters don't work within single quotes.

08:50.160 --> 08:51.270
So let's summarize.

08:51.270 --> 08:58.930
So in this video we talked about the type coercion, and I think it's super important to just realize

08:58.930 --> 09:05.140
how important concept this is, because it can really have some serious consequences.

09:05.140 --> 09:12.250
What we did here, by comparing just this variable value to true, is something that you're gonna do

09:12.250 --> 09:21.490
all the time, because those kind of comparisons are often used in control structures that decide the

09:21.490 --> 09:23.290
flow of the program.

09:23.290 --> 09:31.540
Now, if you wouldn't be aware of the implications of type coercion or just using the strict equality

09:31.540 --> 09:35.950
operators, then well, you just might get into trouble.

09:35.950 --> 09:43.870
So it's super important to just be aware of type coercion that we've also seen in this array example.

09:43.870 --> 09:54.100
And then we've also seen how you can explicitly cast one type to another by using the typecasting operator.
