You've already seen how to pass arguments to functions, but there's plenty more to cover. In this section, you'll look at a technique for giving your arguments default values and explore a method of passing variables by reference rather than by value. This means that the function is given an alias of the original value rather than a copy of it.
Setting Default Values for Arguments
PHP provides a nifty feature to help build flexible functions. Until now, we've said that some functions require one or more arguments. By making some arguments optional, you can render your functions a little less autocratic.
Listing 7.10 creates a useful little function that wraps a string in an HTML span element. We want to give the user of the function the chance to change the font-size style, so we demand a $fontsize argument in addition to the string (line 2).
Listing 7.10. A Function Requiring Two Arguments
Put these lines into a text file called fontwrap.php and place this file in your web server document root. When you access this script through your web browser, it should look like Figure 7.7.
Figure 7.7. A function that formats and outputs strings.
By assigning a value to an argument variable within the function definition's parentheses, we can make the $fontsize argument optional. If the function call doesn't define an argument for this argument, the value we have assigned to the argument is used instead. Listing 7.11 uses this technique to make the $fontsize argument optional.
Listing 7.11. A Function with an Optional Argument
When the fontWrap() function is called with a second argument, as in line 5, this value is used to set the font-size attribute of the span element. When we omit this argument, as in lines 6, 7, and 8, the default value of "12pt" is used instead. You can create as many optional arguments as you want, but when you've given an argument a default value, all subsequent arguments should also be given defaults.
Passing Variable References to Functions
When you pass arguments to functions, they are stored as copies in parameter variables. Any changes made to these variables in the body of the function are local to that function and are not reflected beyond it. This is illustrated in Listing 7.12.
Listing 7.12. Passing an Argument to a Function by Value
Put these lines into a text file called addfive.php and place this file in your web server document root. When you access this script through your web browser, it produces the following:
The addFive() function accepts a single numeric value and adds 5 to it, but it returns nothing. We assign a value to a variable $orignum in line 5 and then pass this variable to addFive() in line 6. A copy of the contents of $orignum is stored in the variable $num. Although we increment $num by 5, this has no effect on the value of $orignum. When we print $orignum, we find that its value is still 10. By default, variables passed to functions are passed by value. In other words, local copies of the values of the variables are made.
We can change this behavior by creating a reference to our original variable. You can think of a reference as a signpost that points to a variable. In working with the reference, you are manipulating the value to which it points.
Listing 7.13 shows this technique in action. When you pass an argument to a function by reference, as in line 6, the contents of the variable you pass ($orignum) are accessed by the argument variable and manipulated within the function, rather than just a copy of the variable's value (10). Any changes made to an argument in these cases will change the value of the original variable. You can pass an argument by reference by adding an ampersand to the argument name in the function definition, as shown in line 2.
Listing 7.13. Using a Function Definition to Pass an Argument to a Function by Reference
Put these lines into a text file called addfive2.php and place this file in your web server document root. When you access this script through your web browser, it produces the following: