Bifalgorithm post · Computer science one · Optional & Default Parameters

How do default parameter values work in PHP functions?

How do default parameter values work in PHP functions? (Computer science one · Optional & Default Parameters) — "PHP also allows you to define alternative defa…

Ideas in this flowchart

  • PHP permite definir valores predeterminados alternativos para los parámetros de una función.
  • La función getMonthlyPayment acepta los parámetros principle, apr y terms.
  • Los parámetros apr y terms tienen valores predeterminados de 0.05 y 60.
  • La función calcula el pago mensual y redondea el resultado a dos decimales.
  • Cuando se omiten parámetros en una llamada, se utilizan sus valores predeterminados.
  • Los parámetros omitidos no se tratan como valores null.
  • Una llamada con todos los argumentos es getMonthlyPayment(10000, 0.07, 72).
  • Esta llamada produce un pago mensual de 170.49.
  • Una llamada que omite terms es getMonthlyPayment(10000, 0.07).
  • En la llamada abreviada, terms toma el valor predeterminado de 60.
Feed/

How do default parameter values work in PHP functions?

No cover

Channel

Computer science one

1 page

"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);"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.