Okay, so now let's learn about the functions in PHP.
That's a really crucial topic to understand functions.
So we're going to be talking about creating our own functions in PHP.
But also PHP offers us the built in functions that are used to work, for example with arrays, strings,
and actually quite a lot of other things.
It's essential to just understand how functions work, what's possible, and what are the types of the
functions.
So you can also download this.
Let's call it a board under this video.
It might look a little bit complex though.
It's actually everything that you need to know about functions.
So we'll be zooming in to some specific parts of it while we work through the topic of functions.
So let's immediately jump to the code editor and write our first function.
So let's begin by creating a new folder for functions that will be functions.
And let's create a new file.
Maybe I can just call it a function because we're going to cover the basics first.
The absolute basics.
Let me add the starting tag.
Okay.
So let's start with how do you define your function.
First you need the function keyword.
And after that you need to come up with a function name.
Then you always add the parenthesis.
And inside those parentheses you can have some optional arguments.
Some functions are fine without arguments, other others require some.
Now with arguments you can use the optional type hinting for every argument like it can be an integer
array may be an object of specific class.
Then some arguments.
They can be optional with optional arguments.
You will provide the default value for them, and also you can have variadic arguments, which means
that the number of arguments is not constant.
You can, for example, pass Past two, 3 or 4 arguments, and then you can optionally define the function
return type after the colon character.
So we're not gonna cover all of this at once inside this video.
Let's just start with a basic function.
So a basic example would be to create a function called greet.
So we can welcome someone and it will accept an argument called name.
Let's keep the types for now.
So then we need to add curly braces.
They are required.
And we would like to return something from this function.
That's one of the best use cases for functions.
You've got a basically a container which has some statements and has a given name.
You pass something to it and it returns something back.
So in this case, I'd like to return a string that would just greet the specific person using the past
name.
A super simple example, but it does the job.
Now I'm going to echo the value returned from this function.
And now how do we call a function.
So that's also simple.
You use the function name and your editor should already suggest this function.
And then you need parentheses.
They are always required.
Now in this case this function requires one argument.
So let's pass something.
This can be a lease.
Now let me go into this new functions folder and then run the function php.
There we have it.
The text was displayed inside the console.
So that was the most basic example of defining a function with arguments and returning a value.
Also, I think it's worth seeing what will happen if you will call a function that has a required argument
without it.
Let's try to call greet without any arguments.
So we run php function PHP.
So what happens is our PHP fatal error, which essentially stops the script at this point.
So one thing to remember required arguments are required.