### MSN Random Online

 Lesson 13 - Creating custom functions
Thursday, 15 February 2007 17:57

# Lesson 13 - Creating custom functions

By now, you should have yourself familiarized with the built-in PHP functions, if you don't, please go back and review them.  Again, the functions are located at http://www.php.net/quickref.php and you are encouraged to read through them and get yourself familiar with the many different actions you can perform with them.

One of the many functions in the PHP documentation is array_sum().  array_sum()'s documentation can be found at http://www.php.net/manual/en/function.array-sum.php.  What does array_sum() do?  It simply adds up all the number types in an array and returns the sum.  You should know how to use this function (from reading the documentation):

Code:
<?
\$array = array(1,5.5,2.0,3,6,1);
echo array_sum(\$array); //prints "18.5"
?>

This is a clear example of what a function is there to do.  This function does a specific task and it saves us from having to repeat code (possibly from summing up several arrays).  Our task for this lesson is the create a NEW function which performs the exact same function as array_sum().  This will help us learn how to implement our own custom functions and also give us a better understanding of why we create functions.

The basic syntax of a function is the following:

function function_name(\$parameter_1,\$parameter_2...\$parameter_n) {
//do something with the parameters
//create a new variable (\$variable)
return \$variable
}

In case you're curious, this is what return does:  When you do "\$variable = array_sum(\$array)", array_sum itself uses the "return" statement which returns a value which in turn you assign to \$variable.

Because we want to keep this as simple as possible, we're going to go about making our function with the assumption that the contents of the array will always only be number types.  The first thing we want to do is create the body of our function.  What does this mean?  This means that if we had a variable (or variables), how would we go about doing the job we want to do?

So let's say we have our variable \$array (of array type), and it contains numbers.  How do we go about adding all the numbers?

Code:
\$variable = 0; //create our new variable to store the sum
foreach (\$array as \$element) { //pretend as if you already have \$array
\$variable += \$element; //add the element to the variable holding the sum
}

We're already half way done; we've created the body of our function.  Let's give our function a name now.  In PHP, you cannot name functions which are built into PHP, or else you'll get an error stating you can redeclare the function.  Since the function we're duplicating is called array_sum(), we'll call our function array_sum_copy().  What variable(s) does our function need as input to do its job?  Well, above, we created the body of our code under the impression that an array (\$array) was declared, so that is what we'll need as input.  Finally, what output does our function return?  Well, we know that the body of our function stores the sum of the array elements inside \$variable, so that's what we'll want to return to the user of the function (using the "return" statement).

Now all we have to do is put our function together:

Code:
function array_sum_copy(\$array) {
\$variable = 0;
foreach (\$array as \$element) { //pretend as if you already have \$array
\$variable += \$element;
}
return \$variable;
}

Let's go back and once again look at what we did.  We selected a name for our function.  We found out what variables our function will need to work (the parameters).  We created the body of our function and used all the parameters of the function.  Finally we figured out what output we want to return and we used the "return" statement to return it.

And this is how it would look if we used it:

Code:
<?

\$array = array(1,5.5,2.0,3,6,1);
echo array_sum_copy(\$array); //prints "18.5"

function array_sum_copy(\$array) {
\$variable = 0;
foreach (\$array as \$element) { //pretend as if you already have \$array
\$variable += \$element;
}
return \$variable;
}

?>

I will be doing one more of these in a few lessons, but I want to just note on a few things.  First, you can create a function that has SEVERAL parameters.  Second, a function can return SEVERAL values (this will be demonstrated later).  Finally, variables declared in a function will only exist in that function.  For example, we declared \$variable to hold the sum of the array elements, but \$variable will only exist within the block it was declared, nowhere else (unless otherwise stated).  This is where we get into scope, and I will be explaining how scope works in PHP in the next lesson.

### Top Members

No top members yet.