PHP Functions

php functions with examples, types of php functions, important php function example list, advance php functions, php built-in functions list, php function optional parameters

PHP Functions

In simple terms, a function is a block of code that can be named and called repeatedly. Sometimes a function is also called a subroutine . We are used to the fact that an ordinary variable can be assigned a number, string or array, and then get it back by referring to the value by the name of the variable. Functions are structured in a similar way. This is also a kind of variable, only instead of a string or number, it contains a block of code that is called when this "variable" is used.

A function is a very powerful code reuse tool. By creating your function and writing the necessary code there, you can call and use it as many times as necessary. Otherwise, you would have to copy and paste the piece of code every time you need it.

To simplify our work, we can form some part of the code in the form of a function, which is used several times in the script. Then, instead of copying and pasting this part of the code, it will be enough just to call this function, as if we were accessing a variable.

There are two types of functions - built-in and  user-defined .

Built-in functions are functions that the creators of the programming language have already written for us, and we can just take them and use them. PHP has thousands of ready-made functions for all occasions!

One of these functions that are already very familiar to us is a function that displays the text passed to it on the screen - print()

The programmer creates custom functions on his own. These functions are usually only used within one project or even a script.

Anatomy of functions

As with regular variables, working with functions consists of declaring and using them.
Before using a new function, you must declare it:

Let's say on our site we want to show if the year selected by the user is a leap year. To do this, we will write a function to which the year is passed. As a result of the function, we want to get the value "true" if the year is a leap year, and "false" if not.
The definition of such a function:
It is important to understand that a function is like a program in a program. This means that variables that were defined outside of it will not be available inside such a function. To pass information from the outside into a function, you need to use the function arguments.

Function arguments  are variables that a function can obtain from external code. In the example with  is_leap_yearsuch a variable there was only one - $year.

Arguments are required because the function "does not see" the variables defined outside of its bounds. Therefore, the required variables must be passed to it explicitly.
The opposite is also true - the variables defined inside the function will not be accessible from the outside. Such variables are called local because they are local to the function.

Unlike arguments, which can be several, a function can return only one value to the external code - using the "return" statement. The return value is called the result of the function.