PHP call function with default arguments -


in many languages can this:

function f(a = 0, b = 0, c = 0) {    // }  f(b=3, c=4); 

can in php?

thank indeed!

no, unfortunately can't "step over" arguments in php, default values possible @ end of list.

so can write this:

 function foo($a=0, $b=0) {}  foo(42); 

here $b have default value $a won't, provided 1 value input.

however there's no way of providing value $b without providing 1 $a - have provide value $b second parameter, , php has no keyword can use in place of first parameter "use default".

there talk of adding named parameters future version of php, similar non-php example.

you can simulate bit changes code, though; couple of ideas:

  • treat null meaning "use default", , write $a = is_null($a) ? 42 : $a
  • make functions take associative array parameter, , take values though keys parameter names

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -