WEBVTT

00:00.080 --> 00:04.910
Next up, let's cover variadic function arguments.

00:04.910 --> 00:09.710
So so far we've covered the required and optional arguments.

00:09.800 --> 00:18.140
If you define variadic arguments on a function, then your function doesn't really have the predefined

00:18.170 --> 00:20.180
amount of arguments.

00:20.180 --> 00:29.540
It means that once you define this variadic argument, which basically is defined by adding three dots

00:29.540 --> 00:39.920
before the argument name, this function accepts unlimited amount of arguments, and everything that

00:39.920 --> 00:48.560
you pass after the required arguments will be passed to this function as an array.

00:49.280 --> 00:51.170
That's the theory.

00:51.200 --> 01:00.200
Now let's see how this works in practice and when this is useful and why it might be useful.

01:00.200 --> 01:04.980
So let's begin by creating another file for the specific case.

01:05.010 --> 01:07.500
Let me call this variadic.

01:07.530 --> 01:08.310
PHP.

01:10.170 --> 01:17.040
As usual, let's add a PHP tag and we're going to begin by defining a new function.

01:17.040 --> 01:25.230
Let's have something simple a sum function that for every single argument passed to this function,

01:25.230 --> 01:28.860
will just return the sum of all of them.

01:29.070 --> 01:33.030
So how do you define a variadic argument?

01:33.060 --> 01:36.330
You add three dots and give it a name.

01:36.330 --> 01:38.190
Let's call it numbers.

01:38.190 --> 01:45.000
So as I've said before this will be passed to this function as an array.

01:45.030 --> 01:45.480
Okay.

01:45.510 --> 01:49.410
So let's calculate the sum of all of those numbers.

01:49.620 --> 01:51.600
So we did that before.

01:51.600 --> 01:55.710
We can use the for each loop for this case.

01:55.710 --> 02:01.230
Or we can also use a predefined function existing in PHP.

02:01.260 --> 02:01.800
Maybe.

02:01.800 --> 02:03.270
Let's go with the loop.

02:03.930 --> 02:11.790
Define the sum variable and then do for each numbers as number.

02:13.650 --> 02:17.970
And let's add the number to the sum.

02:20.730 --> 02:24.630
The dollar sign and return the sum.

02:24.960 --> 02:33.630
So if a function accepts variadic arguments, it means that you can either pass 0 or 5 or 10 or 20 arguments

02:33.630 --> 02:34.140
to it.

02:34.170 --> 02:43.170
Let's try to call this function first with nothing, and maybe let's vardump everything so we see what's

02:43.170 --> 02:44.790
the exact result.

02:45.630 --> 02:50.910
Okay so I'm inside the functions directory so I can call variadic PHP.

02:52.740 --> 02:57.840
So we've got zero because well this was an empty array.

02:58.620 --> 03:02.760
Now let me also var dump the numbers.

03:04.060 --> 03:08.650
So we can confirm that we actually have received an array.

03:09.310 --> 03:10.330
There it is.

03:11.950 --> 03:12.160
Okay.

03:12.190 --> 03:15.430
So next up let's actually add some arguments.

03:15.430 --> 03:19.870
Let me add one argument and run this function again.

03:21.310 --> 03:29.500
So this time this is what this function received an array where we have just one item an integer five.

03:29.590 --> 03:31.990
So that's the obvious result.

03:32.470 --> 03:32.830
Okay.

03:32.860 --> 03:40.690
So now let's add some more three maybe four or even five arguments.

03:42.580 --> 03:44.260
So you see how it works.

03:45.040 --> 03:49.810
We can now pass any amount of arguments to that function.

03:49.810 --> 03:53.530
And this function will receive an array.

03:55.390 --> 03:55.630
Okay.

03:55.630 --> 03:57.460
So let's have another example.

03:57.490 --> 04:02.290
Let's mix the required arguments with variadic arguments.

04:02.320 --> 04:13.960
So let's define another function, call it introduce team, where you're gonna accept the team name

04:14.320 --> 04:17.320
and members.

04:19.330 --> 04:24.460
So here we've got one required argument that should be a string.

04:24.460 --> 04:29.560
We are not working with types yet, but well, we basically expect the team name here.

04:29.560 --> 04:34.510
And then you can have well any amount of members.

04:35.950 --> 04:43.030
So here we're gonna just echo the team name like that.

04:43.150 --> 04:48.520
And next up we're gonna just list all the members.

04:48.820 --> 04:54.220
So it's the first time we're gonna use a built in function that does something with arrays.

04:54.220 --> 04:57.130
This is something you might really use quite often.

04:57.130 --> 04:58.780
It's called implode.

04:58.780 --> 05:05.570
So what this function does is it converts an array to a string.

05:05.600 --> 05:08.240
The first argument is the glue.

05:08.240 --> 05:12.860
So what should be added after every single array element?

05:12.890 --> 05:15.590
Let's add a comma and a space.

05:15.620 --> 05:18.260
And the second argument has to be an array.

05:18.260 --> 05:22.370
And we know that members is a variadic argument.

05:22.370 --> 05:24.680
Thus it's an array.

05:25.760 --> 05:29.720
So let's also join that with a new line character.

05:29.900 --> 05:33.890
As usual we might want to dump the members.

05:33.890 --> 05:36.500
So we see that it is indeed an array.

05:36.530 --> 05:37.430
And that's it.

05:37.430 --> 05:41.870
This function doesn't return anything, it just echoes something.

05:41.870 --> 05:44.090
Let me call this now.

05:44.570 --> 05:47.390
So this first parameter is required.

05:47.390 --> 05:51.380
So maybe let me call this a team.

05:52.910 --> 05:59.900
And then we have John Mr. T maybe that's enough.

05:59.900 --> 06:04.880
And now let's just call PHP variadic PHP.

06:05.120 --> 06:07.340
And this is our team.

06:07.520 --> 06:12.500
And that's the argument, the variadic argument we've received.

06:12.680 --> 06:15.950
It's an array because we have well, it's always an array.

06:15.950 --> 06:23.480
But it contains two elements because we've passed two elements after the required argument.

06:23.480 --> 06:25.430
And that's the output.

06:27.350 --> 06:27.650
Okay.

06:27.650 --> 06:33.380
So that was an example of mixing the required arguments with variadic arguments.

06:34.550 --> 06:37.760
Next up let's introduce the type hinting here.

06:37.760 --> 06:42.320
So previously I've said that we should be using strict types.

06:42.320 --> 06:43.490
Let's do that.

06:44.840 --> 06:49.880
So we define declared the strict types equals one.

06:49.910 --> 06:53.090
I'm adding the semicolon even though it's optional.

06:53.090 --> 06:57.890
And now how can we make every item in this array.

06:57.920 --> 07:01.280
Well maybe an integer with the sum.

07:01.280 --> 07:05.270
This actually makes sense.

07:05.270 --> 07:14.160
So essentially you can add the type before the variadic parameter variadic argument, and this would

07:14.160 --> 07:22.950
force every single element passed to this function, to this variadic argument to be an integer, which

07:22.980 --> 07:27.330
essentially would force us to have an array of integers.

07:27.330 --> 07:32.430
And if you would try to pass something else, you're gonna have an error.

07:33.600 --> 07:35.280
So we've got this example here.

07:35.280 --> 07:39.690
And if I just run it, it should run fine.

07:39.690 --> 07:49.020
But if I'm going to change this to a string, then we've got a fatal error because it needs to be of

07:49.020 --> 07:59.940
type int as this type before this variadic argument defines the type for every single variadic argument.

08:00.000 --> 08:07.530
So since this function only has this variadic argument, then everything that you pass to it need to

08:07.560 --> 08:09.330
be an integer.

08:12.360 --> 08:12.630
Okay.

08:12.630 --> 08:16.620
So also let's make sure that we have types everywhere.

08:16.620 --> 08:21.270
So in this case in the team that's a string members.

08:21.270 --> 08:23.580
Well obviously this would be names.

08:23.580 --> 08:31.350
So we require every single member to be a string not a number or an array or anything else.

08:31.350 --> 08:39.570
And keep in mind or notice that this function here does not return anything with such cases.

08:39.570 --> 08:43.560
We should also have a return type that would be void.

08:43.830 --> 08:49.080
That's the return type that you add when the function does not return anything.

08:49.110 --> 08:52.350
And why this is useful?

08:52.350 --> 08:58.260
Well, if I would try to return something instead of echoing it, let's see what will happen.

09:00.540 --> 09:03.690
As you see, I even have an error right away.

09:03.900 --> 09:10.930
And it says that a void function must not return a value.

09:11.140 --> 09:18.670
So if you don't plan to return a value from a function, well, you should add the void type for extra

09:18.700 --> 09:19.840
safety.

09:20.200 --> 09:24.070
Let's see if everything works fine here.

09:24.100 --> 09:26.320
Yes, it works fine.

09:26.890 --> 09:35.170
If I would pass a number instead of John, then this should also trigger an error.

09:35.470 --> 09:36.460
And it did.

09:36.910 --> 09:39.790
So let's bring it back to the working state.

09:41.530 --> 09:48.430
Now, finally, what if we have an array like our, like an array of numbers?

09:48.430 --> 09:58.030
And we'd like to pass this elements from this array into a sum function to calculate a sum.

09:59.140 --> 10:00.640
Can we do that easily?

10:00.670 --> 10:07.630
Well, maybe the brutal way would be to just pass every single argument explicitly like this.

10:07.660 --> 10:09.700
So first element of our array.

10:09.700 --> 10:15.730
a second element of array, etc. but there is obviously a better way.

10:15.880 --> 10:19.630
So it's called unpacking arrays into arguments.

10:19.630 --> 10:21.790
So given those numbers.

10:22.450 --> 10:31.330
Let's vardump the result I can call sum and use this three dot operator on the array.

10:31.360 --> 10:33.700
That's variable or expression.

10:33.700 --> 10:41.560
And what this would do is it will take all those elements from this array and pass them as subsequent

10:41.560 --> 10:44.650
variadic arguments of that function.

10:47.020 --> 10:54.940
So if I will call this right now, we can see that I think that's this case right here.

10:55.240 --> 11:01.930
So we received an array with three integers one, two and three.

11:02.230 --> 11:04.270
So that's this case.

11:04.300 --> 11:10.360
Now you can also do this unpacking together with the regular arguments.

11:10.370 --> 11:14.930
So if I would try to introduce another theme.

11:18.080 --> 11:22.040
Maybe here I'm just going to have the members array.

11:22.670 --> 11:34.100
Um, so let me do Harry and maybe Johnny and Joe.

11:35.930 --> 11:45.500
I can call, introduce team, name the team and just unpack the members.

11:46.130 --> 11:49.100
Let's run it to see if this has worked.

11:49.100 --> 11:51.590
So this is our B team.

11:51.680 --> 11:54.590
This is what this function has received.

11:54.620 --> 11:58.430
Harry, John and Joe, that seems to be matching our array.

11:58.430 --> 12:04.550
And there is the output the members which also looks fine.

12:06.260 --> 12:06.470
Okay.

12:06.500 --> 12:15.470
So maybe one more example can we actually Mix the explicit arguments that we pass to this variadic argument

12:15.470 --> 12:19.130
with maybe unpacking the arrays.

12:19.610 --> 12:21.200
Well let's see.

12:21.200 --> 12:23.390
So let's call this function.

12:23.390 --> 12:30.740
And the way I want to call it is I'm gonna just pass some explicit values.

12:30.740 --> 12:37.160
So I'm just gonna pass those strings that will go into this variadic argument of the function.

12:37.160 --> 12:40.730
And I also add unpacking at the end.

12:41.900 --> 12:43.640
Will this work.

12:44.210 --> 12:45.560
Let's run the function.

12:45.590 --> 12:46.970
Now obviously this works.

12:46.970 --> 12:56.630
That's this example team B maybe I'm gonna just call it um not here team C so it's clear that's the

12:56.630 --> 13:08.120
team C and we've got those two explicit guys I've passed John and Mr. T and the members that came from

13:08.120 --> 13:08.720
the array.

13:08.720 --> 13:11.720
So as you see, we can mix those.
