WEBVTT

00:00.440 --> 00:00.800
Okay.

00:00.800 --> 00:03.650
So let's learn about the union type.

00:04.460 --> 00:07.070
So we're going to learn with functions.

00:07.070 --> 00:10.370
So let's create a union PHP file.

00:10.370 --> 00:14.270
And let's explain what it is all about.

00:14.900 --> 00:18.050
So as usual we're going to create a function.

00:18.860 --> 00:22.670
So let me call this process input.

00:23.270 --> 00:30.650
And the thing is that the input the argument it can take different types.

00:30.650 --> 00:35.210
So first let's declare the strict types here.

00:37.580 --> 00:38.540
Like this.

00:38.540 --> 00:41.990
And let me add the function body.

00:42.560 --> 00:48.200
So the input I'd like it to be either integer but also float and string.

00:48.440 --> 00:52.370
How can we achieve that if I've already typed that as int?

00:52.370 --> 00:56.060
Well this is where union types are for.

00:56.990 --> 00:58.220
So I use this.

00:58.250 --> 01:02.380
Let's call it pipe Type character or alternative character.

01:02.380 --> 01:06.160
And this way I just define alternative types.

01:06.190 --> 01:11.380
So if I want int, float and string, this is how I do that now.

01:11.410 --> 01:15.010
All those types can be passed to this function.

01:15.010 --> 01:23.440
But if I need to then work with this data that is being passed, it's my job to just check what's the

01:23.440 --> 01:24.040
type.

01:24.070 --> 01:26.680
Obviously if I need this information.

01:27.340 --> 01:34.000
Next up, depending on the type of the passed argument, we'd like to pass it through some different

01:34.000 --> 01:35.740
processing rules.

01:35.740 --> 01:42.850
And maybe let's again use the match expression after we initially explained it.

01:42.850 --> 01:45.760
I don't think we have ever used it again.

01:45.760 --> 01:48.250
So let me show you something.

01:48.250 --> 01:53.470
So we can use match expression by just matching the true value.

01:53.470 --> 02:02.100
So then in every individual case, I can actually call any function Like I can call the is int function

02:02.100 --> 02:13.830
to check if the input is an integer, and then I can output something something like integer and concatenate

02:14.130 --> 02:16.740
the input text.

02:16.770 --> 02:19.830
Maybe I will just multiply this integer.

02:19.950 --> 02:25.650
Next up I'm gonna check if it is a float.

02:26.970 --> 02:37.320
Then we're gonna return the text float and concatenate that with rounding the input.

02:38.640 --> 02:42.570
And finally let's check if it is a string.

02:42.570 --> 02:49.830
So if input is a string then we are just outputting that it is a string.

02:49.830 --> 02:53.370
And maybe let's cast it to uppercase.

02:53.370 --> 02:59.860
So we use the str to upper function On the input.

03:02.080 --> 03:07.240
And the default case would be we just output unknown type.

03:07.600 --> 03:15.160
Now that is an interesting use case for the match expression because we are matching true and we want

03:15.160 --> 03:18.490
to match it to a case that will also return true.

03:18.550 --> 03:26.800
Now all of those cases can't return true because the argument can't be integer, float, and string

03:26.800 --> 03:31.510
at the same time, or it can't be none of those at the same time.

03:31.780 --> 03:37.840
So it's actually only possible to match one of those cases to true.

03:37.870 --> 03:44.020
So that just might be an not really obvious use case for the match expression.

03:44.740 --> 03:44.920
Okay.

03:44.950 --> 03:47.800
So now let's use this function for something.

03:47.800 --> 03:51.430
Let's define an array with different values.

03:51.670 --> 03:56.740
So an integer float value a string.

03:56.740 --> 04:07.030
Another integer, maybe another float value, and finally a string.

04:08.080 --> 04:13.540
And then let's use a loop to go over all the elements.

04:15.430 --> 04:20.260
That's inputs as input.

04:22.150 --> 04:30.070
And for every single one of those elements from the array, let's just echo the output of process input,

04:30.490 --> 04:31.900
passing the input.

04:31.900 --> 04:35.860
And let's just concatenate that with a new line.

04:35.860 --> 04:38.200
And I think it's time to run it.

04:38.200 --> 04:48.580
I run the union PHP and this is our output, which basically just confirms that this function can work

04:48.580 --> 04:49.750
with union types.

04:49.750 --> 04:51.430
And also as a summary.

04:51.580 --> 04:53.980
That's just the way you define union types.

04:53.980 --> 04:56.860
If obviously you have a need for that.
