PHP Function
Creating and invoking function
To create a function you can simply follow the following patterns:
function function_name(arguments){
// function body code
}
You start with the keyword function and followed by function's name. Inside the bracket you put one ore more arguments seperated by a comma. For example here is the function to issue the header of a web page.
<?php
// write the header
function writeHeader(){
print("<p>Site Header</p>");
}
// invoke function
writeHeader();
?>
To invoke a function, you simply put the function name and bracket like above code snippet.
Passing by value
Let 's take a look at an example:
<?php
function increase($x){
$x = $x + 1;
echo 'inside function x = ' . $x .'<br />';
}
$x = 10;
increase($x);
echo 'outside function x = ' . $x .'<br />';
?>
In the above code snippet, you see we have function increase(). Basically this function increase any arguments passed to by 1 and print the value. In the line 6 we have a variable $x then in line 7 we pass $x to the function increase(). After that we print the value of $x. Here is the output:
inside function x = 11
outside function x = 10
So the value of $x did not change by the function increase(). It's value chnaged only inside the function. In this case we say passing by value. The function does not change the value of the passed arguments.
Passing by reference
In case you want to change actual value of passed argument, you need to pass it to the function by reference. We can redefine our function above again as follows:
<?php
function increase(&$x){
$x = $x + 1;
echo 'inside function x = ' . $x .'<br />';
}
$x = 10;
increase($x);
echo 'outside function x = ' . $x .'<br />';
?>
We put the ampersand in front of argument we want to pass by reference. The value of x in this case changed not only inside the function but also has effect outside the function as well. If we pass argument in this maner we say passing by reference.
Default argument values
PHP function allows you pass default value for argument. Let's take a look at an example:
<?php
function increase(&$x,$inc = 1){
$x = $x + $inc;
}
$x = 10;
increase($x,10); // 20
echo $x;
increase($x); // 21
echo $x;
?>
We use the same function increase() which accepts two arguments. The second argument is the number which we want to increase by. To set default value of this argument we put equal and default value after this argument. In line 7 we call function as normal. In line 9 we call the function but omit the second argument. In this case 1 is default value used. The second argument is also called optional argument.
Returning values
In most of the cases, you function will process the input arguments and returns values to the calling function or program. You use return to return value from a function. Consider the following function:
<?php
// find max of two values
function max($a,$b){
if ($a > $b)
return $a;
else
return $b;
}
$m = max(10,20);
echo $m; //20
?>
The function find maximum value of two input arguments.
PHP function do allows you to return multiple values from function. Let's take a look at an example:
<?php
function rgb(){
$rgb = array("red","green","blue");
return $rgb;
}
$color = rgb();
// print array
print_r($color);
//Array ( [0] => red [1] => green [2] => blue )
?>
The function rgb() returns an array which contains multiple values.