PHP Function

Summary: in this tutorial, you will learn how to make your code more organized and reusable by using PHP function.

A function is a named block of code that is designed to perform a specific task. Once a function is defined, you can reuse it without copying and pasting a code block again and again. A function may accept one or more arguments, which are the values that you pass to the function. A function may return a value so that the calling script can communicate with it.

Creating and calling functions

To create a function, you use the following syntax:

A function starts with the keyword function and is followed by the function name. A function name can be any string that begins with a letter or underscore ( _) followed by zero or more letters, underscores and numbers. Function names are case-insensitive. It means count(), Count() and COUNT() refer to the same function. By convention, PHP built-in functions are in lowercase.

Inside the brackets () after the function name, you specify one or more parameters. If a function accepts more than one parameter, each parameter has to be separated by a comma.

The code inside the curly brace {} is a function body. You can put PHP code, HTML code or mixed PHP and HTML code inside its body. For example, the following function displays the header of a web page:


To call a function, you simply put the function name and parentheses () as follows:

Function parameters

Function may accept one or more parameters. There are two ways to pass parameters to a function: passing parameter by value and passing parameter by reference.

Passing parameters by value

Let’s take a look at the following example:

How it works.

  • We created a new function named increase() with one parameter. Inside the function, we increased the value of the $x parameter by 1 and displayed it.
  • Outside the function, first, we declared a variable named $x and initialized its value to 10. Second, we called the increase() function and passed $x to it. Third, we displayed the $x variable again.

The following is the output of the script:

So the value of $x did not change by the increase() function. It only changed inside the function. In this case we say we pass $x to the increase() function by value. The function actually works on a copy of the argument.

Passing parameters by reference

Passing by reference allows you to access the variable directly instead of its copy so that you can change its value inside a function. To pass parameters to a function by reference, the arguments must be variables, and you add ampersands ( &) in front of the arguments. For example, we can redefine the increase() function as follows:

We put an ampersand ( &) in front of the argument that we want to pass by reference, $x. By passing by reference, the value of $x changed not only inside but also outside the function.

Default parameters values

PHP function allows you to pass default values to its parameters. To specify a default value for a parameter, you assign the parameter a scalar value in the function declaration. Notice that only a scalar value is accepted as default value for a parameter.

Let’s take a look at the following example:

How it works.

  • First, we created the increase() function that accepts two arguments. The second argument is a number by which we increase the first argument. To set default value for the $inc parameter, we used assignment operator ( =) and default value ( 1) after its name.
  • Second, we called the increase() function by passing two arguments.
  • Third, we called the increase() function, however this time we omitted the second argument. In this case, the default value 1 is used.

The argument with default value is also known as optional argument.

Variable parameters

Sometimes it is useful if the number of parameters is not fixed e.g., a get_total() function that calculates total of any number of passing parameters. To achieve this, you need to use three handy PHP built-in functions:

  • func_get_args() returns an array of parameters.
  • func_num_args() returns the number of parameters.
  • func_get_arg(arg_num) returns a specific argument from the parameters.

The following example demonstrates how to use those functions to develop the get_total() function.

Type hinting

PHP introduced type hinting since version 5. Type hinting allows a function to force parameters to be objects of particular classes, interfaces, arrays or callable.

Notice that type hinting cannot be used with scalar such as strings or integers, or traits.

See the following example:

You will learn about classes and objects in PHP classes and objects tutorial.

Returning values

A function typically processes the arguments and returns a value to the caller, which may be another function or script. To return a value from a function, you use the return statement.

When PHP encounters the return statement, it terminates the function immediately and returns the value back. Let’s take a look at the following example:

The find_max() function returns the maximum value of two arguments.

If you want to return multiple values, you can return an array. See the following example:

The rgb() function returns an array that contains multiple values.

By default a function return NULL if it does not specify any return value.

A function can return a reference instead of a value. To return a reference from a function, you precede the function name with an ampersand ( &):

Nested function

In PHP, a function can be nested inside another function. Let’s take a look at the following example:

The find_max() function is used to find the maximum value of three arguments. Inside the find_max() function, we have another function named max() defined, which is called nested function, that finds maximum values of two input arguments.

We find the maximum value of the first and second argument using the nested function, max(). Then, we find the maximum value of the maximum value returned from the max function and the third argument using the nested function, max().

Variable functions

PHP allows you to store a name of a function in a string variable and use the variable to call the function. Variable functions are useful in cases that you want to execute functions dynamically at run-time.

The following example demonstrates how to use variable functions:

First, we defined two functions: find_min() and find_max() . Then, we called those function via the $f string variable. Notice that you need to put parentheses after the variable in order to call the function specified by the value of the variable.

Anonymous functions

We can define a function that has no specified name. We call this function is an anonymous function or a closure. We often use anonymous functions as values of callback parameters.

For example, the usort() function allows you to sort an array by its values using a user-defined comparison function. We can pass an anonymous function to the usort() function as the following example:

We defined an anonymous function that compares two strings by their lengths. The usort() function sorts the $a string array by order of the string length, which specified by the anonymous function.

Benefits of using function

Functions are very important in PHP. There are several benefits of using functions as following:

  • Functions are reusable – Because a function is designed to perform a specific independent task so that it can be reused in other web applications.
  • Functions help avoid duplicate code – A function helps avoid copying and pasting code all over places by wrapping the logic and assigning it a name.
  • Functions make your script modular – by using functions, a big script is divided into many functions that are easier to build, test and maintain.

This tutorial covered all aspect of PHP function including creating function, function parameters, returning values, closure and benefits of using functions.

  • Was this tutorial helpful ?
  • YesNo