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:
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 |