WEBVTT

00:00.290 --> 00:03.980
Okay, so now let's learn about the functions in PHP.

00:04.370 --> 00:09.290
That's a really crucial topic to understand functions.

00:09.290 --> 00:14.090
So we're going to be talking about creating our own functions in PHP.

00:14.210 --> 00:23.900
But also PHP offers us the built in functions that are used to work, for example with arrays, strings,

00:23.900 --> 00:27.440
and actually quite a lot of other things.

00:27.590 --> 00:34.400
It's essential to just understand how functions work, what's possible, and what are the types of the

00:34.400 --> 00:35.210
functions.

00:35.210 --> 00:37.940
So you can also download this.

00:38.180 --> 00:40.850
Let's call it a board under this video.

00:40.850 --> 00:43.670
It might look a little bit complex though.

00:43.670 --> 00:47.390
It's actually everything that you need to know about functions.

00:47.390 --> 00:55.100
So we'll be zooming in to some specific parts of it while we work through the topic of functions.

00:55.100 --> 00:59.990
So let's immediately jump to the code editor and write our first function.

01:00.950 --> 01:08.120
So let's begin by creating a new folder for functions that will be functions.

01:09.650 --> 01:12.440
And let's create a new file.

01:12.470 --> 01:17.990
Maybe I can just call it a function because we're going to cover the basics first.

01:18.020 --> 01:20.210
The absolute basics.

01:20.690 --> 01:23.690
Let me add the starting tag.

01:25.190 --> 01:25.460
Okay.

01:25.460 --> 01:28.880
So let's start with how do you define your function.

01:28.970 --> 01:31.490
First you need the function keyword.

01:31.490 --> 01:35.630
And after that you need to come up with a function name.

01:35.630 --> 01:38.450
Then you always add the parenthesis.

01:38.450 --> 01:43.250
And inside those parentheses you can have some optional arguments.

01:43.250 --> 01:49.100
Some functions are fine without arguments, other others require some.

01:49.100 --> 01:57.590
Now with arguments you can use the optional type hinting for every argument like it can be an integer

01:57.620 --> 02:01.460
array may be an object of specific class.

02:01.460 --> 02:03.980
Then some arguments.

02:03.980 --> 02:07.550
They can be optional with optional arguments.

02:07.550 --> 02:14.990
You will provide the default value for them, and also you can have variadic arguments, which means

02:14.990 --> 02:19.160
that the number of arguments is not constant.

02:19.160 --> 02:29.550
You can, for example, pass Past two, 3 or 4 arguments, and then you can optionally define the function

02:29.550 --> 02:33.870
return type after the colon character.

02:34.110 --> 02:39.060
So we're not gonna cover all of this at once inside this video.

02:39.060 --> 02:41.760
Let's just start with a basic function.

02:43.800 --> 02:48.360
So a basic example would be to create a function called greet.

02:48.900 --> 02:53.910
So we can welcome someone and it will accept an argument called name.

02:53.910 --> 02:56.130
Let's keep the types for now.

02:56.130 --> 02:59.040
So then we need to add curly braces.

02:59.040 --> 03:00.540
They are required.

03:00.660 --> 03:04.200
And we would like to return something from this function.

03:04.200 --> 03:08.670
That's one of the best use cases for functions.

03:08.940 --> 03:15.750
You've got a basically a container which has some statements and has a given name.

03:15.750 --> 03:19.410
You pass something to it and it returns something back.

03:19.410 --> 03:27.150
So in this case, I'd like to return a string that would just greet the specific person using the past

03:27.180 --> 03:27.840
name.

03:27.870 --> 03:30.840
A super simple example, but it does the job.

03:30.870 --> 03:35.610
Now I'm going to echo the value returned from this function.

03:35.610 --> 03:38.190
And now how do we call a function.

03:38.190 --> 03:39.840
So that's also simple.

03:39.840 --> 03:44.880
You use the function name and your editor should already suggest this function.

03:45.480 --> 03:47.670
And then you need parentheses.

03:47.670 --> 03:49.860
They are always required.

03:49.890 --> 03:53.940
Now in this case this function requires one argument.

03:53.940 --> 03:55.650
So let's pass something.

03:55.830 --> 03:57.390
This can be a lease.

03:57.390 --> 04:02.910
Now let me go into this new functions folder and then run the function php.

04:03.210 --> 04:04.620
There we have it.

04:05.910 --> 04:08.400
The text was displayed inside the console.

04:08.400 --> 04:16.290
So that was the most basic example of defining a function with arguments and returning a value.

04:17.010 --> 04:24.210
Also, I think it's worth seeing what will happen if you will call a function that has a required argument

04:24.210 --> 04:25.410
without it.

04:25.440 --> 04:30.780
Let's try to call greet without any arguments.

04:31.140 --> 04:34.230
So we run php function PHP.

04:34.530 --> 04:41.430
So what happens is our PHP fatal error, which essentially stops the script at this point.

04:41.430 --> 04:45.930
So one thing to remember required arguments are required.
