Specify parameter by name in PHP -


i have no idea called, i've seen php supports cool parameter passing like:

function myfunc($required_value, $optional_value1 = 'default', $optional_value2 = 'default2'){  } 

and i'd able do:

myfunc('something', $optional_value2 = 'test'); 

so 2 questions in regards this:

  1. what technique called (for future references)?
  2. since php version supported?

its called default parameters.

in exapmple. there slight mistake. given function

function myfunc($required_value, $optional_value1 = 'default', $optional_value2 = 'default2'){ ..... } 

you can call in following ways:

myfunc('required_value'); //$optional_value1 = 'default', $optional_value2 = 'default2' myfunc('required_value', 'opt1'); //$optional_value2 = 'default2' myfunc('required_value', 'opt1', 'op2'); 

thing note php doesn't support named parameters, there order important. therefore, can't omit middle params. following statements wrong because trying use named parameters.

myfunc('something', $optional_value2 = 'test'); 

for more details, see this


Comments