How to Use Functions in PHP
PHP has a number of built-in functions that can be used to perform specific tasks. You can also create your own custom functions. A function is a block of code that can be called and executed multiple times within a script. Functions are not executed automatically when a script is loaded, they must be called and executed in order to execute.
To call a function, simply reference the name of the function, followed by the arguments, inside curly brackets
Another important thing to note about functions is that they scope by default. This means that a variable or value inside a function represents a different value/memory space than the same variable outside of the function. To change this behavior, you can use the global keyword in your function definition.
For example, in the following code sample we have a function defined called optionalDemo that expects two values as arguments. In this case, the first argument is referenced as $greeting and the second argument is referred to as $countwithin the function. If the function is called without providing a value for the second parameter, then the loop in the function will be executed twice. To avoid this issue, we can specify that the second parameter is to be referred to as $countwithin the functions definition by using the global keyword.
Another useful feature of the return statement in PHP is that you can declare a type for the variable that you are returning from a function. This allows you to have stricter type checking that the return variable matches the type of argument that it was called with.