|
|
Functions
A function is a block of code designed to accomplish a certain task that can be executed, or called, by other parts of a script. You may have heard functions referred to as subroutines, procedures, or methods in other languages. The concept is identical.
We have already seen functions in use. The strtoupper() function in the second part of this tutorial is one such example. In this part, we learn how to create functions of our own. The basic syntax for creating a function is:
function functionName() {
//Do something.
}
Functions should be used when you have a specific task which needs to be performed repeatedly - Using a function, you need only write the code for that task once, and can then simply refer to it each time you want to perform the task.
Adding Arguments
Often, we need to pass arguments to a function. Arguments are variables which are made available to the code within the function. The strtoupper() function, for example, has a string as an argument. It processes this string and returns a copy of it, with any lower-case letters replaced by their upper-case equivalents. Let's look at a simple example:
<?php
function times7($number) {
return $number * 7;
}
for($i=1; $i<=5; $i++) {
$multiple = times7($i);
echo("7 multiplied by $i is $multiple.<br>");
}
?>
The result:
7 multiplied by 1 is 7.
7 multiplied by 2 is 14. 7 multiplied by 3 is 21. 7 multiplied by 4 is 28. 7 multiplied by 5 is 35.
Here, we create a function, times7(), which has one argument, $number. The return statement allows the function to calcuate a result and pass it back to the main body of code. In this case, we multiply the argument by 7, and then assign it, as if it were a variable or any other value, to the $multiple variable. Only one variable can be returned by a function. If you need to return several pieces of data, you can return an array, or pass some arguments by reference, which is discussed later.
Multiple Arguments
More arguments can be added by separating them with a comma:
function lotsOfArguments($arg1, $arg2, $arg3) {
}
Optional Arguments
Sometimes, we don't want to have to specify the value of an argument each time. Arguments can be made optional by specifying a default value for them when we create the function:
<?php
function multiplyNumbers($first, $second, $echoResult=false){
$result = $first * $second;
if($echoResult==true){
echo($result);
}
return $result;
}
$calc1 = multiplyNumbers(2, 3); //By not specifying a value for $echoResult, the default of false is assumed.
$calc2 = multiplyNumbers(3, 4, true); //Here we do specify a value, which will override the default.
?>
Passing by Reference
By default, arguments in PHP are passed by value - if a variable is used as an argument to a function, a copy of that variable is created temporarily for use within the function, and destroyed when the function returns. Consider the following:
<?php
function addOne($number) {
$number++;
}
$a = 0;
echo($a . "<br>");
addOne($a);
echo($a . "<br>");
?>
The result will be:
0
0
Why? When we pass the $a variable to the function, PHP creates a copy of it (called $number), increments it by one as it should, and then destroys it when the function ends. The original variable remains untouched. In this simple example, we could of course have written the following:
<?php
function addOne($number) {
$number++;
return $number;
}
$a = 0;
echo($a . "<br>");
$a = addOne($a);
echo($a . "<br>");
?>
Here, the copy is incremented, returned, and assigned back to the $a variable. Sometimes, however, we may need a function to modify a variable directly. Or, we may just want to return 2 values instead of one, without resorting to an array. In these cases, we can use a reference:
<?php
function addOne(&$number) {
$number++;
}
$a = 0;
echo($a . "<br>");
addOne($a);
echo($a . "<br>");
?>
The output now is:
0
1
Using an ampersand (&) before the variable in the function's argument list tells PHP to access this variable directly rather than making a copy of it. Thus, when we increment the function's $number variable, we are in fact incrementing the $a variable which was passed to it.
Page Responses
Currently there have been no responses to this page...
If you have anything to contribute to this tutorial, found a bug, or know a better way of achieving the same goal, please leave your response below.
|