As you become more comfortable with writing code, you might realize that many times you will write the same bits of code over and over again. A prime example of this is the database connection code used throughout this book. How many times did you think to yourself, "This is really repetitive!" Quite often, I'm sure. This is where writing your own functions comes into play.
When you program in PHP, you will use predefined functions to achieve certain results. For example, the mail() function is a predefined function that sends mail. The mysql_connect() function is a predefined function that connects to a MySQL database. The code that makes up these functions is built into the PHP scripting engine, so you never see it. However, you can write your own functions and use them in your scripts, even storing your own functions in external files for use only when you need them.
Functions have a very specific structure, as you can see in the following code, where [function name] and [arguments] should be replaced with your own function name and any arguments you might want to use:
function [function_name] ([arguments]) {
// code
}
When you create a function, you precede the name of the function with the literal word function. After that and the name of your function comes the list of arguments inside a set of parentheses. The arguments—which are optional, because you don't have to pass any arguments to a function if you don't want to— are separated by commas, and hold values that your function needs in order to complete its task or tasks.
After the arguments, you open a set of curly braces, type all of your code, and finally close the set of braces. For example, the following function (called multiplier) takes an argument called $num and multiplies the value by 5.
function multiplier ($num) {
$answer = $num * 5;
}
Say you have already determined that $num equals 8, and that's what you're passing to the multiplier function. Using your own math skills, you know that $answer will equal 40. To get that number back to your script, outside of the function, you must return the value.
The basic method for returning values from your functions is to use the return statement. Usually, this statement comes at the end of the function, like so:
| Note |
A return statement can be anywhere in a function, but when used, it ends the execution of the function. This means the code that is executed is the line of your script from which the return statement was called. |
When you use a return statement, you can then call the function in your code like so:
echo multiplier(5);
This usage would result in the following on your screen:
40
Because you are passing 8 as the $num argument to the multiplier() function, $answer becomes 40. Because $answer is being returned as the result of the function's actions, and you are using echo followed by the function call, you're telling PHP to print the result of the code within the function. In this case, that result is the number 40.
| Note |
You can also use the return statement to get multiple values, but only if they are part of an array or an object. |
Using a return statement to pass results from your functions to your main script is a simple and safe method, and one of the most common. If you do not use a return statement, you must declare as global any variables you want to pass back to your main script. For example:
In this case, you call the multiplier() function and then use the name of the variable in the echo statement, because it's not returned directly from your function. For example, using the modified function, the following code will print the number 40 to your screen:
multiplier(5); echo $answer;
If you had not declared $answer as a global variable, the result would have been a blank screen.
| Note |
Use some thought to determine which variables you really want to make globally available to your scripts. Each time you declare something as global, you must employ additional programming constraints in order to maintain the integrity of the data. In other words, you have to be careful and watch what you do. If you have declared a variable as global, but you use a variable of the same name in another part of your script, you will overwrite one or the other. A good rule of thumb is to keep a handle on your global variables, and keep them local to the procedures that directly use them. |