Setting default argument values

I l @ ve RuBoard

While writing functions, PHP allows you to preset a value for your arguments. The function will use this default value unless it receives an argument which would then overwrite the default. In other words, by setting a default value for an argument, you have rendered that particular argument optional when calling the function.

Using the CalculatePayments() function as an example (Script 9.7), you could set 12 as the number of monthly payments by creating your function like this:

 function CalculatePayments ($Amount,  $NumberPayments = "12") { 

Calling the function with the code CalculatePayments ($Amounts); would still work without a problem but coding CalculatePayments ($Amounts, "24"); would set the $NumberPayments variable equal to 24, not the default 12.

You would set an argument's default value if you wanted to assume a certain value but still allow for other possibilities. However, keep in mind that the default arguments should always be written after the other standard arguments (those without defaults). This is because PHP directly assigns values to arguments in the order they are received from the call line. Thus, it is not possible to omit a value for the first argument but include one for the second (which would therefore mean that you sent one value which would automatically be equated to the first argument, not the second).For example, if a function was written as this:

 function CalculateTotal ($HowMany,  $Price = "20.00", $TaxRate = "0.06") { 

and you called the function with this line:

 CalculateTotal (3, "0.07"); 

with the intention of setting $HowMany to 3, leaving $Price at 20.00 and changing the $TaxRate to 0.07, there would be problems. The end result would be that $HowMany gets set to 3, $Price gets set to 0.07 and $TaxRate remains at 0.06 which is not the desired result. The proper way to achieve that affect would be to code:

 CalculateTotal (3, "20.00", "0.07"); 

Let's rework the numbers .php page (Script 9.7) to incorporate the notion of setting default argument values.

To write a function that uses default values:

  1. Open numbers.php (Script 9.7) in your text editor, if it is not already.

  2. Add a default value to the $Savings variable in the CalculateTotal() function (Script 9.8):

    Figure 9.8. The numbers.php page is now written as you commonly would write a function. Setting the default value within a function makes an argument optional.

    graphics/09sc08.jpg

     function CalculateTotal ($HowMany,  $Savings = "0") { 
  3. You have now set the value of $Savings to be 0, as a default. If no argument is passed to the function, it will be assumed that no discount applies. Edit the second function, setting the $NumberPayments argument to the default of 12.

     function CalculatePayments ($Amount,  $NumberPayments = "12") { 

    If two arguments are sent to the CalculatePayments() function, the $NumberPayments will be set to the second value instead of the default.

  4. Delete the two lines in the main body of the script that printed out the value of $Tax, since they are no longer relevant.

  5. Alter the call to the CalculatePayments function so that it no longer sends along an argument for the number of payments.

     print (CalculatePayments  ($TotalCost); 

    The CalculatePayments() call now only passes the $TotalCost value as an argument, since $NumberPayments has a default value. You could pass a $NumberPayments value here if you wanted to set a different number than 12 as the number of monthly payments.


    Figure .

    graphics/09sc08a.jpg


  6. Save your script, upload it to the server, and test it in your Web browser (Figure 9.6). Remember to send the script a Quantity value (and a Discount value, if you want)by appending it to the URL or via an HTML form.

    Figure 9.6. Even though the end user , viewing your pages in their browser, will never be able to tell the difference between scripts which use functions and those which do not (compare this to Figure 9.4), you've increased the usability of your programming tenfold.

    graphics/09fig06.gif

Tip

In every example in this chapter, I pass arguments to functions by value, which means that a copy of the value of a variable is sent to the function, not the actual variable itself. You can also pass arguments by reference, which allows you to modify a variable in a function, but that's too complex a topic for this book. See Appendix C, PHP Resources , for more on where you can learn about this subject.


Tip

When it comes to naming conventions for function arguments, there are different schools of thought. On the one hand, using the same name in the function as a variable outside of the function makes it easy to remember how values match up. On the other hand, using generic argument names in your function makes it less script-specific. You could also use a hybrid method by appending a lower case f to the beginning of function names such as $fHowMany and $fTaxRate. Some programmers identify global variables as $gVariable. Again, the key is to remain consistent to your naming policy.


I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

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