3.4 Building Functions
Functions are the key to reusability. You have already learned to call PHP's built-in functions in this chapter but up to now you have not implemented your own functions. In this section you will learn to build your own PHP functions.
3.4.1 Simple Functions
Let's start with a simple example: Gauss's Formula for the sum of integers.
Johann Carl Friedrich Gauss was born on April 30, 1777, in Brunswick, Germany. At the age of seven, Carl Friedrich Gauss started elementary school, and his potential was noticed almost immediately. His teacher, Büttner, and his assistant, Martin Bartels, were amazed when Gauss summed the integers from 1 to 100 instantly by spotting that the sum was 50 pairs of numbers each pair summing to 101. Gauss's Formula for the sum of integers was born.
<?php $result = gauss(4); echo "Sum from 1 to 4: $result<br>\n"; # function for calculating the sum from 1 to $upper function gauss($upper) { if (is_int($upper) && ($upper > 0)) { return($upper*($upper+1)/2); } } ?>
In the first line you can see that the function Gauss is called. One parameter has to be passed to the function. This parameter will define the upper limit for the sum you want to compute.
If the parameter passed to the function is an integer value and if it is higher than zero, the result is computed and returned to the main function. The result is passed to the main function by using the return command.
In this case 10 will be returned and displayed on the screen:
Sum from 1 to 4: 10
Functions can also be called with more than just one parameter. The next example shows a slightly adapted version of the function you have seen before. With the help of this function it is possible to compute the sum of integer values from a starting value to an upper value (the starting value is not included in the sum):
<?php $lower = 4; $upper = 10; $result = gauss($lower, $upper); echo "Result: $result<br>\n"; function gauss($lower, $upper) { if ($upper >= $lower && $lower >= 0) { return ($upper*($upper+1)/2) - ($lower*($lower+1)/2); } } ?>
If you execute the script, the result will be displayed on the screen:
Result: 45
3.4.2 Passing Arrays to a Function
Sometimes it is necessary to pass entire arrays to a function. PHP does not support function overloading, but with the help of arrays it is possible to build simple workarounds.
The target of the next example is to compute the geometric mean of an array of values:
<?php $values = array(4, 3, 19, 23, 15); $result = geomean($values); echo "Result: $result<br>\n"; function geomean($invalues) { foreach ($invalues as $val) { $sum += $val*$val; } return sqrt($val); } ?>
First, an array containing various values is created. In the next step the function called geomean is called and the array is passed to it. Geomean computes the sum of all values to a power of 2 in the array. After that the square root is computed and returned to the main function. The result will be displayed on the screen:
Result: 3.8729833462074
3.4.3 Functions Returning More Than One Value
Up to now, you have dealt with functions returning just one value. However, it is possible for one function to return many values.
Let's have a look at the next example:
<?php $values = array(4, 3, 19, 23, 15); $result = compute($values); foreach ($result as $val) { echo "val: $val<br>\n"; } function compute($invalues) { $result = array(); $sum = 1; foreach ($invalues as $val) { $sum += $val*$sum; array_push($result, $sum); } return $result; } ?>
First, an array is generated and passed to the function called compute, which performs some simple operations and returns an array of values.
The output is displayed on the screen:
val: 5 val: 20 val: 400 val: 9600 val: 153600
As you can see, it makes no difference if you want to return an ordinary variable or an array of valuesthe syntax is the same.