Introduction to functions

A function is in essence a piece of code which can be reused multiple times in the same program. Typically a function will return a value usually from one or more inputs. In this introduction to functions in PHP we'll cover the basics: the syntax of functions, basic parameters, returning values and using functions in your code.

The Syntax

A basic function, without any parameters, takes the following syntax:

<?php
function Function_Name() {
    
// function code goes here
}
?>

The function name, in this case simply Function_Name, must start with either a letter or underscore (_) and can only contain letters, numbers and underscores.

Parameters

A parameter is a variable that is passed to a function to be used within the code. Parameters are added to the above function syntax like so:

<?php
function Function_Name($parameter1$parameter2) {
    
// function code goes here
}
?>

At the moment values for both of these parameters will need to be sent when we call the function. However we can set a default value for a parameter:

<?php
function Function_Name($parameter1 'hello'$parameter2 'world') {
    
// function code goes here
}
?>

In this case the default values for $parameter1 and $parameter2 are "hello" and "world" respectively. If no value for either of these parameters is passed when we call the function then these values will be used.

The code

Next we'll move on to writing the function code. If a function has parameters then these will be avaliable for the function to use. In the example below, the function Add_Numbers adds the two parameters $num1 and $num2 and stores the result in a variable called $total

<?php
function Add_Numbers($num1$num2) {
    
$total $num1 $num2;
}
?>

As it stands this function is next to useless as $result is only avaliable inside the function. In order for us to be able to get the result we return the value. This is done using the return statement:

<?php
function Add_Numbers($num1$num2) {
    
$total $num1 $num2;
    return 
$total;
}
?>

If we wanted to output the result we could also echo() the result:

<?php
function Add_Numbers2($num1$num2) {
    
$total $num1 $num2;
    echo 
$total;
}
?>

Using functions

Now the functions have been written we can be begin to use them. Firstly in order to call a function (which will execute the code within it) we simply write the function name together with two brackets and finish with a semicolon like any other line of PHP code:

<?php
Function_Name
();
?>

This will run the Function_Name function. If the function has parameters we need to pass values or variables for the parameters:

<?php
Add_Numbers
(35);

$numbera 3;
$numberb 5;

Add_Numbers($numbera$numberb);
?>

This will call the Add_Numbers function (above) with the values 3 and 5 set as the parameters $num1 and $num2.

If the function outputs (echos, prints, etc) a value itself then calling a function like above is fine. However when calling a function which returns a value requires some further processing to output the value

<?php
$result 
Add_Numbers(35);
echo 
$result// will outputs 8

echo(Add_Numbers(35)); // outputs 8
?>

So what is the point in returning values? The reason is to use the value in futher processing, for example:

<?php
$result 
Add_Numbers(35);

$answer $result 10;

echo 
$answer// outputs 18
?>

And that is PHP functions!