Concept
How do default parameter values work in PHP functions?
ComputerScienceOne / Optional & Default Parameters
"PHP also allows you to define alternative default values for function parameters. Consider the following example.\n\nfunction getMonthlyPayment($principle, $apr = 0.05, $terms = 60) {\n\n$rate = ($apr / 12);\n\n$mp = (($principle * $rate) / (1-pow(1+$rate, -$terms)));\n\nreturn round($mp * 100) / 100;\n\n}\n\nIn this example, the second and third parameter have been given default values of 0.05 and 60 respectively. If a call to this function omits these parameters, they are not treated as null, but take on these defaults instead.\n\n$x = getMonthlyPayment(10000, 0.07, 72);\n\nprint $x; //170.49\n\n// terms will be 60\n\n$x = getMonthlyPayment(10000, 0.07);"
Related Ideas
- How do optional and default function parameters work in PHP?ComputerScienceOne · Generalized Rounding
- How do omitted arguments match parameters in PHP functions?ComputerScienceOne · Function Pointers
- What are the key features of functions in PHP?ComputerScienceOne · Declaring Functions
- How can PHP default values let a constructor accept different numbers of parameters?ComputerScienceOne · Usage
- How do you declare functions and specify their return values in PHP?ComputerScienceOne · Declaring Functions
- How does PHP handle undefined variables, null values, and constants?ComputerScienceOne · Using Variables
- How do you call a function in PHP?ComputerScienceOne · Calling Functions
- How do you call a PHP function with values or variables?ComputerScienceOne · Organizing Functions