More About Arguments


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 font element. We want to give the user of the function the chance to change the font element's size attribute, so we demand a $size argument in addition to the string (line 2).

Listing 7.10. A Function Requiring Two Arguments
 1: <?php 2: function fontWrap($txt, $size) { 3:     echo "<font size=\"$size\">$txt</font>"; 4: } 5: fontWrap("A Heading<br>",5); 6: fontWrap("some body text<br>",3); 7: fontWrap("some more body text<br>",3); 8: fontWrap("yet more body text<br>",3); 9: ?> 

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.


Useful though this function is, we really only need to change the font size occasionally. Most of the time we use the default value of 3. By assigning a value to an argument variable within the function definition's parentheses, we can make the $size argument optional. If the function call doesn't define an argument for this, the value we have assigned to the argument is used instead. Listing 7.11 uses this technique to make the $size argument optional.

Listing 7.11. A Function with an Optional Argument
 1: <?php 2: function fontWrap($txt, $size = 3) { 3:     echo "<font size=\"$size\">$txt</font>"; 4: } 5: fontWrap("A Heading<br>",5); 6: fontWrap("some body text<br>"); 7: fontWrap("some more body text<br>"); 8: fontWrap("yet more body text<br>"); 9: ?> 

When the fontWrap() function is called with a second argument, as in line 5, this value is used to set the size attribute of the font element. When we omit this argument, as in lines 6, 7, and 8, the default value of 3 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
 1: <?php 2: function addFive($num) { 3:      $num += 5; 4: } 5: $orignum = 10; 6: addFive($orignum); 7: echo $orignum; 8: ?> 

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:

 10 

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
 1: <?php 2: function addFive(&$num) { 3:      $num += 5; 4: } 5: $orignum = 10; 6: addFive($orignum); 7: echo $orignum; 8: ?> 

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:

 15 



Sams Teach Yourself PHP MySQL and Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (4th Edition)
ISBN: 067232976X
EAN: 2147483647
Year: 2003
Pages: 333
Authors: Julie Meloni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net