Library functions have already been demonstrated in this tutorial but it's also possible to create your own PHP functions.
Functions provide a way to group PHP statements into blocks that are reusable - making for more compact code and easier maintenance.
Functions are first defined then called within the script in the same way as library functions. Functions can be defined anywhere within a script but it's normal to define them at the start.
A function may have no arguments (sometimes called parameters) or it may have many. A Function may also return arguments.
Defining a Function
A function begins with the word function followed by a unique name for the function.
The name is followed by opening and closing parenthesis with arguments, separated by commas, inside - if there are any.
This is followed with an opening brace then the PHP statements that make up the body of the function
A closing brace is used to end the function definition
Example of a function() Definition
<?php
function highlight ($string)
{
echo("<b><i>".strtoupper($string)."</i></b>");
}
$this_string = "warning";
// This prints out WARNING in bold italic
highlight ($this_string);
?>
Returning Values
A function can use the return statement to pass back values as in the example below.
Argument types aren't fixed until the function is called and the return type is determined only when it's actually returned.
Variable Scope
When a variable is created, its value is not automatically available at every point in the programme.
There is a relationship between where it is created and where it can be used. This relationship is called the variable's scope.
When a variable is created inside a function it only has scope inside the function. It only exists when the function is called and is discarded when the function ends.
/* This doesn't print out anything because
$result has no scope outside the function */
$number = 10;
multiply ($number);
echo ($result);
?>
This is true even if the variable inside the function has the same name as one outside. Although they have the same name they are not the same variable as shown below.
/* This prints out 10 because $result inside
the function is different to $result outside */
$number = 10;
multiply ($number);
echo ($number);
?>
Variables used within a function are local to the function with two exceptions.
When variables are declared as global.
When variables are passed to functions by reference.
Global Variables
If a variable is declared as global inside a function it means that it is the same as the variable outside the function.
Example of Global
<?php
function multiply ($number)
{
global $number;
$number = $number * 2;
}
/* This prints out 20 because $result inside
the function is the same as $result outside */
$number = 10;
multiply ($number);
echo ($number);
?>
Note: Global should only be used where absolutely necessary. When you give a function the right to alter a variable you pass a lot of power to the function.
When global is used in lots of functions in a large programme, it can be very difficult to remember which functions are changing a variable.
Passing Variables by Reference
By default only the value of a variable is passed to a function - not the variable itself.
But the variable may be passed by reference by using the & ampersand character before it.
This means that the variable outside the function with the same name is affected by what happens to the variable inside the function.