User-Defined PHP Functions

We have been learning about all kinds of useful functions that come built-in with PHP, but what if you can't find a function to perform a certain task? What if it does not exist? PHP allows you to build your own functions.

Why build a function? Why not just write the code needed to perform the task and move on? If you think you might use a block of code more than once, it will save a lot of time and effort to put it in a function and call the function as many times as necessary later on in the page. You can even store your function(s) in a separate file and include the file on as many pages as needed.

To create a function, the syntax is: function name() { }

You can name a function anything you want, as long as the function name is not already in use as a PHP function. Function names are not case sensitive, and can include letters, numbers and/or underscores, but can only begin with letters or underscores. Let's take a look at a simple function:

<?php
  function test() {
    echo "I am in a function!";
  }
?>

So what does this function do? Nothing! Why? Because it has not yet been called. Until a function is called, it will sit around looking bored (or boring), waiting to be of use... Let's learn how to call our poor function and give it something to do:

<?php
  function test() {
    echo "I am in a function!";
  }

  test();
?>

Simple! Too simple? Probably. All that the function does now that it is called is echo a single statement. Let's try something a bit more complex.

<?php
  function mathy_stuff($a, $b) {
    echo $a * $b;
  }

  mathy_stuff(5, 7);
  mathy_stuff(3, 100);
  mathy_stuff(7000, 68);
?>

Now, when our new function is defined, we have two parameters, or variables, between the function's parentheses. These variables are used in the function, where they are multiplied by one another and echoed. The variables are not actually assigned their values until the function is called, at which point the values are listed in the function's parentheses, comma-separated in the order of the variables they need to be assigned to. A function can handle as many (or few) comma-separated parameters as you (the programmer) can keep track of, if necessary.

Returning values allows you to store the results of a user-defined function in a variable. Consider the following:

<?php
  function example($a, $b) {
    $total = $a + $b;
    return $total;
  }

  $addition = example(50, 51);
  echo $addition . " dalmations!";
?>

As you can see, instead of echoing the result of $a + $b, we store it in a variable and then return that variable. (A function can only return one thing, so choose carefully!) Then, the function is called as the value of the $addition variable, and is assigned two values to use in the function. At last, the $addition variable (containing the results of the function) is echoed along with a short string of text.

Some people consider functions hard to get a handle on, but it is not really much harder than learning how to ride a bike, and it is a lot easier on the knees!