WEBVTT

00:00.680 --> 00:08.780
So far, we've been passing the arguments to functions by having to remember about the order of every

00:08.780 --> 00:10.310
single argument.

00:10.430 --> 00:14.360
So in this video, we're going to learn about the named arguments.

00:14.360 --> 00:20.180
That's the most recent PHP feature introduced in PHP 8.0.

00:20.180 --> 00:29.630
And it basically lets you pass the arguments by specifying the argument name argument value combination

00:29.690 --> 00:32.900
instead of having to remember about the order.

00:32.930 --> 00:42.920
So let's create an example a grid function with a name with the greeting, which can have a default

00:42.920 --> 00:49.820
value, and whether someone should shout, which also has a default value.

00:49.820 --> 00:52.280
And this all returns a string.

00:52.310 --> 01:01.970
Okay, so now let's just create a message based on greeting and the name.

01:02.780 --> 01:06.950
And we're going to return that whether we need to shout.

01:06.980 --> 01:08.720
We use the ternary operator.

01:08.720 --> 01:21.820
So if this is true then we return the option on the right which would be str are two upper, so we uppercase

01:21.820 --> 01:26.050
the message or after the column.

01:26.050 --> 01:28.270
If the shout is false.

01:28.570 --> 01:33.760
This would be returned and that would just be the original message.

01:33.970 --> 01:38.500
Now let's see an example of this with positional arguments.

01:38.530 --> 01:40.960
So I can greet Alice.

01:42.790 --> 01:47.260
Maybe let's concatenate this with a new line.

01:49.570 --> 02:01.810
Then another example can be saying hi to Alice, and yet another example can be saying hey and saying

02:01.810 --> 02:03.550
that someone should shout.

02:03.730 --> 02:06.910
So obviously this works and it's perfectly fine.

02:06.910 --> 02:14.950
But the problem maybe I need to go to the functions folder, but the only problem is that you really

02:14.950 --> 02:17.620
need to remember the order of arguments.

02:17.620 --> 02:25.870
And while this generally is a good practice to create functions that do not have a lot of arguments,

02:26.170 --> 02:32.290
if you have ten arguments, you are probably doing something wrong and should separate that into couple

02:32.360 --> 02:33.230
Functions.

02:33.470 --> 02:38.480
Anyway, it is hard to remember the order of functions.

02:38.480 --> 02:43.220
And let's see how can we do that with named arguments instead?

02:44.000 --> 02:52.730
So I'm doing grid function call, and now I can set the name to David.

02:52.730 --> 02:55.940
And I can set that David.

02:56.540 --> 03:00.440
Or the welcome message to David should be shouted.

03:02.090 --> 03:08.030
So when I now run named PHP, as you see, this works perfectly fine.

03:08.180 --> 03:17.030
But now I don't have to worry about the position of arguments, which also lets me ignore the arguments

03:17.030 --> 03:18.830
that have default value.

03:18.860 --> 03:25.940
Otherwise, um, I would have to provide the value for the second argument, even though it has the

03:25.940 --> 03:30.140
default value, and I might want to use the default one.

03:30.410 --> 03:36.740
And that's the case with positional arguments to provide the value for the third argument, you can't

03:36.740 --> 03:38.300
skip the second one.

03:38.300 --> 03:42.170
And with named arguments this is possible.

03:42.860 --> 03:47.270
And I also think this is much easier to remember.
