PHP Programming/Functions

Introduction
Functions (or methods in the context of a class/object) are a way to group common tasks or calculations to be re-used simply.

Functions in computer programming are much like mathematical functions: You can give the function values to work with and get a result without having to do any calculations yourself.

You can also find a huge list of predefined functions built into PHP in the PHP Manual's function reference.

How to call a function
Note that echo is not a function. "Calling a function" means causing a particular function to run at a particular point in the script. The basic ways to call a function include:
 * calling the function to write on a new line (such as after a ";" or "}")
 * calling the function to write on a new line inside a control
 * assigning the returned value of a function to a variable "$var = function"
 * calling a function inside the argument parentheses (expression) of a control

In our earlier examples we have called several functions. Most commonly we have called the function print to print text to the output. The parameter for echo has been the string we wanted printed (for example print("Hello World!") prints "Hello World!" to the output).

If the function returns some information, we assign it to a variable with a simple assignment operator "=":

Parameters
Parameters are variables that exist only within that function. They are provided by the programmer when the function is called and the function can read and change them locally (except for reference type variables that are changed globally, which is a more advanced topic).

When declaring or calling a function that has more than one parameter, you need to separate between different parameters with a comma ','.

A function declaration can look like this:

To call this function, you must give the parameters a value. It doesn't matter what the value is, as long as there is one:

Output:

Hello World

When declaring a function, you sometimes want to have the freedom not to use all the parameters. Therefore, PHP allows you to give them default values when declaring the function:

These values will only be used, if the function call does not include enough parameters. If there is only one parameter provided, then $var2 = "I'm Learning PHP":

Output:

Hello I'm Learning PHP

Another way to have a dynamic number of parameters is to use PHP's built-in func_num_args, <tt>func_get_args</tt>, and <tt>func_get_arg</tt> functions.

or

The above functions would calculate the arithmetic mean of all of the values passed to them and output it. The difference is that the first function uses <tt>func_num_args</tt> and <tt>func_get_arg</tt>, while the second uses <tt>func_get_args</tt> to load the parameters into an array. The output for both of them would be the same. For example:

Output:

Mean: 27

Returning a value
This function is all well and good, but usually you will want your function to return some information. Generally there are two reasons why a programmer would want information from a function:
 * 1) The function does tasks such as calculations, and we need the result.
 * 2) A function can return a value to indicate, if the function encountered any errors.

To return a value from a function use the <tt>return</tt> statement in the function.

Example PHP script:

Result:

The result of 1 + 6 + 9 is 16

Notice that a <tt>return</tt> statement ends the function's course. If anything appears in a function declaration after the <tt>return</tt> statement is executed, it is parsed but not executed. This can come in handy in some cases. For example: Notice that there is no <tt>else</tt> after the <tt>if</tt>. This is due to the fact that, if <tt>$divider</tt> does equal <tt>0</tt>, the <tt>return</tt> statement is executed and the function stops.

If you want to return multiple variables, you need to return an array rather than a single variable. For example:

When calling this from your script, you need to call it into an array. For example:

In this case <tt>$return['tot']</tt> will be the total (e.g. 15), while <tt>$return['diff']</tt> will be the difference (5).

Runtime function usage
A developer can create functions inside a PHP script without having to use the <tt>function name($param...) {}</tt> syntax. This can be done by way of programming that can let you run functions dynamically.

Executing a function that is based on a variable's name
There are two ways to do it: either using the direct call, or the <tt>call_user_func</tt> or the <tt>call_user_func_array</tt>:

Using <tt>call_user_func*</tt> functions to call functions
<tt>call_user_func</tt> and <tt>call_user_func_array</tt> only differ in that the <tt>call_user_func_array</tt> allows you to use the second parameter as array to pass the data very easily, and <tt>call_user_func</tt> has an infinite number of parameters that is not very useful in a professional way. In these examples, a class will be used for a wider range of using the example:

Using <tt>call_user_func</tt>:

Using <tt>call_user_func_array</tt>:

Note how <tt>call_user_func</tt> and <tt>call_user_func_array</tt> are used in both of the examples. <tt>call_user_func_array</tt> allows the script to execute the function more dynamically.

As there was no example of using both of these functions for a non-class function, here they are:

Using <tt>call_user_func</tt>:

Using <tt>call_user_func_array</tt>:

Creating runtime functions
Creating runtime functions is a very good way of making the script more dynamic:

<tt>create_function</tt> creates a function with parameters <tt>$one</tt> and <tt>$two</tt>, with a code to evaluate return… When <tt>create_function</tt> is executed, it stores the function's info in the memory and returns the function's name. This means that you cannot customise the name of the function although that would be preferred by most developers.