php 101

Custom Functions

So far we've been working with functions built into PHP itself. Those are essential, but you'll often need to perform some custom processing on a string, variable, number, or object before you can use it. And you may need to perform that custom processing from many places in your web application. To avoid duplicating code all over the place, you'll want to wrap up the statements from your custom processing into a function you write yourself, which you can then call from anywhere. Building on our previous examples, let's say you want to transform a block of text to uppercase and wrap its lines to 20 characters, inserting <br /> tags after each line. Perhaps not very realistic, but useful for our purposes. You'll need to do two things:

  • Define the function with a name and set of arguments that it takes
  • Call it from elsewhere in your script, supplying the required arguments

In this case, our custom function will take a single argument -- a paragraph stored in a variable.

Let's break it down. On line 2, we define a new function called "convert_string" and specify that it takes a single argument, which will be referred to inside the function as $mystring. Note that this does not mean we have to pass variables called $mystring into the function - the function will take any string variable. $mystring will be what we refer to the passed-in variable as from within the function.

On lines 4 and 5, we use the built-in functions strtoupper() and wordwrap() just as we have in earlier examples. On line 4, we store the result of the strtoupper() as a new variable called $tempstring. On line 5, we pass the new $tempstring variable (which is by this point in all uppercase text) to wordwrap(), overwriting the $tempstring variable. $tempstring is now both wrapped and uppercased.

Line 6 is something new -- a "return" statement is how a function creates output. Functions may do hundreds of things while they're running, but they generally output just one thing. The return statement is how you tell the function what it needs to give back to your program -- in this case, the doubly converted string that started as simple paragraph input.

On lines 15 and 17, we declare some simple string variables. These would more likely be paragraphs coming out of a database, but for our purposes, we're hard-coding them. On lines 20 and 21 we echo them back just so we can compare the originals to the converted versions on the page.

On lines 24 and 25 we put our custom function to work by calling the function by name and passing the raw strings to our function as arguments. Remember, whatever is in the "return" statement of the function will be that function's output. We nest the call inside an echo() statement here, but we could also have stored it in another variable for further processing.

Custom functions can get much more complex than this of course, but these examples should give you a good sense of how functions work, and how easy it is to write your own.

Experiment: What happens if you call the convert_string() function with no arguments at all? How might you prevent your program from crashing if you were to make that mistake?