Next up, let's cover variadic function arguments.
So so far we've covered the required and optional arguments.
If you define variadic arguments on a function, then your function doesn't really have the predefined
amount of arguments.
It means that once you define this variadic argument, which basically is defined by adding three dots
before the argument name, this function accepts unlimited amount of arguments, and everything that
you pass after the required arguments will be passed to this function as an array.
That's the theory.
Now let's see how this works in practice and when this is useful and why it might be useful.
So let's begin by creating another file for the specific case.
Let me call this variadic.
PHP.
As usual, let's add a PHP tag and we're going to begin by defining a new function.
Let's have something simple a sum function that for every single argument passed to this function,
will just return the sum of all of them.
So how do you define a variadic argument?
You add three dots and give it a name.
Let's call it numbers.
So as I've said before this will be passed to this function as an array.
Okay.
So let's calculate the sum of all of those numbers.
So we did that before.
We can use the for each loop for this case.
Or we can also use a predefined function existing in PHP.
Maybe.
Let's go with the loop.
Define the sum variable and then do for each numbers as number.
And let's add the number to the sum.
The dollar sign and return the sum.
So if a function accepts variadic arguments, it means that you can either pass 0 or 5 or 10 or 20 arguments
to it.
Let's try to call this function first with nothing, and maybe let's vardump everything so we see what's
the exact result.
Okay so I'm inside the functions directory so I can call variadic PHP.
So we've got zero because well this was an empty array.
Now let me also var dump the numbers.
So we can confirm that we actually have received an array.
There it is.
Okay.
So next up let's actually add some arguments.
Let me add one argument and run this function again.
So this time this is what this function received an array where we have just one item an integer five.
So that's the obvious result.
Okay.
So now let's add some more three maybe four or even five arguments.
So you see how it works.
We can now pass any amount of arguments to that function.
And this function will receive an array.
Okay.
So let's have another example.
Let's mix the required arguments with variadic arguments.
So let's define another function, call it introduce team, where you're gonna accept the team name
and members.
So here we've got one required argument that should be a string.
We are not working with types yet, but well, we basically expect the team name here.
And then you can have well any amount of members.
So here we're gonna just echo the team name like that.
And next up we're gonna just list all the members.
So it's the first time we're gonna use a built in function that does something with arrays.
This is something you might really use quite often.
It's called implode.
So what this function does is it converts an array to a string.
The first argument is the glue.
So what should be added after every single array element?
Let's add a comma and a space.
And the second argument has to be an array.
And we know that members is a variadic argument.
Thus it's an array.
So let's also join that with a new line character.
As usual we might want to dump the members.
So we see that it is indeed an array.
And that's it.
This function doesn't return anything, it just echoes something.
Let me call this now.
So this first parameter is required.
So maybe let me call this a team.
And then we have John Mr. T maybe that's enough.
And now let's just call PHP variadic PHP.
And this is our team.
And that's the argument, the variadic argument we've received.
It's an array because we have well, it's always an array.
But it contains two elements because we've passed two elements after the required argument.
And that's the output.
Okay.
So that was an example of mixing the required arguments with variadic arguments.
Next up let's introduce the type hinting here.
So previously I've said that we should be using strict types.
Let's do that.
So we define declared the strict types equals one.
I'm adding the semicolon even though it's optional.
And now how can we make every item in this array.
Well maybe an integer with the sum.
This actually makes sense.
So essentially you can add the type before the variadic parameter variadic argument, and this would
force every single element passed to this function, to this variadic argument to be an integer, which
essentially would force us to have an array of integers.
And if you would try to pass something else, you're gonna have an error.
So we've got this example here.
And if I just run it, it should run fine.
But if I'm going to change this to a string, then we've got a fatal error because it needs to be of
type int as this type before this variadic argument defines the type for every single variadic argument.
So since this function only has this variadic argument, then everything that you pass to it need to
be an integer.
Okay.
So also let's make sure that we have types everywhere.
So in this case in the team that's a string members.
Well obviously this would be names.
So we require every single member to be a string not a number or an array or anything else.
And keep in mind or notice that this function here does not return anything with such cases.
We should also have a return type that would be void.
That's the return type that you add when the function does not return anything.
And why this is useful?
Well, if I would try to return something instead of echoing it, let's see what will happen.
As you see, I even have an error right away.
And it says that a void function must not return a value.
So if you don't plan to return a value from a function, well, you should add the void type for extra
safety.
Let's see if everything works fine here.
Yes, it works fine.
If I would pass a number instead of John, then this should also trigger an error.
And it did.
So let's bring it back to the working state.
Now, finally, what if we have an array like our, like an array of numbers?
And we'd like to pass this elements from this array into a sum function to calculate a sum.
Can we do that easily?
Well, maybe the brutal way would be to just pass every single argument explicitly like this.
So first element of our array.
a second element of array, etc. but there is obviously a better way.
So it's called unpacking arrays into arguments.
So given those numbers.
Let's vardump the result I can call sum and use this three dot operator on the array.
That's variable or expression.
And what this would do is it will take all those elements from this array and pass them as subsequent
variadic arguments of that function.
So if I will call this right now, we can see that I think that's this case right here.
So we received an array with three integers one, two and three.
So that's this case.
Now you can also do this unpacking together with the regular arguments.
So if I would try to introduce another theme.
Maybe here I'm just going to have the members array.
Um, so let me do Harry and maybe Johnny and Joe.
I can call, introduce team, name the team and just unpack the members.
Let's run it to see if this has worked.
So this is our B team.
This is what this function has received.
Harry, John and Joe, that seems to be matching our array.
And there is the output the members which also looks fine.
Okay.
So maybe one more example can we actually Mix the explicit arguments that we pass to this variadic argument
with maybe unpacking the arrays.
Well let's see.
So let's call this function.
And the way I want to call it is I'm gonna just pass some explicit values.
So I'm just gonna pass those strings that will go into this variadic argument of the function.
And I also add unpacking at the end.
Will this work.
Let's run the function.
Now obviously this works.
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
team C and we've got those two explicit guys I've passed John and Mr. T and the members that came from
the array.
So as you see, we can mix those.