PHP Functions Tutorial

A function is simply a block of code that is executed when it is called upon. You can pass variables (which are called arguments) to it and you can have the function return a value. In general, if you use a particular piece of code (or sequence of statements) in your script more than once, it is usually a good idea to write a function containing that code and call it as necessary.

Functions are a neat way of simplifying your code since you only have to write the code once, and once it is working properly, you can easily call it from anywhere in your code. This makes your code more modular and readable.

Think about the echo statement. You just write the statement and tell it the data you want to put on the screen and it does the rest for you. A function is like that, whereby you put your own custom code in it and then run all that code by calling it in your script. Look at the examples in the PHP tutorial below.

PHP Function Syntax

The structure for writing a function in PHP is shown directly below:

function functionName($arg1, $arg2, ...., $argn)
{
    // some code that gets executed
    // ...
    // ...
    return someVariable;
}

The variables called $arg are optional and so is the return statement. We'll look at these in our examples below. Also, a function name can start with a letter or an underscore, but not a number.

PHP Function Example

You simply declare the function and write the commands you want to be executed inside it. You then call the function by name from within your regular program and the code gets executed automatically.

<?php
 
    // declare the function
    function printHeader()
    {
        echo 'Welcome to Tutorial Arena';
    }
 
    // call the function
    printHeader();
?>

The result:

Welcome to Tutorial Arena

We first declared a function called printHeader and then called it from inside the body of our code. Note that the code inside of the function curly braces is not executed until the function is called by name. That is, even though the code is there, it will be ignored if you do not make a call to the function.

PHP Function that takes parameters

Below is an example of a function that takes parameters.

<?php
 
    function printSum($num1, $num2)
    {
        echo ($num1 + $num2);
    }
 
    printSum(5, 6);
?>

The result:

11

Look at the example directly above. We pass some additional information when calling the function in the form of the variables $num1 and $num2. This makes these values available to the function, and is a useful way of changing the behavior of the function from outside. That is, the function will still add 2 numbers, but we are able to specify any two numbers when calling the function.

Note that the same names that the variables are given in the function declaration are the same names to use inside of the function to retrieve the values.

PHP Function that returns something

We can also write a function that does a calculation and then returns that result to the main program. We make a function return a value using the return statement as shown below.

<?php
 
    function getSum($num1, $num2)
    {
        return ($num1 + $num2);
    }
 
    $sum = getSum(5, 6);
    echo $sum;
 
?>

The result:

11

This function is a bit different. The actual output on the screen does not happen in the function. Rather, it is returned to the main script which handles what to do with it, in this case echoes it.

That was a basic introduction to functions in PHP. Look at the code in the examples and try to write some code for yourself. Remember, practice makes perfect.

When you are comfortable with the concept of a function, feel free to move on to another PHP tutorial in our series.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...