PHP - Calling functions with multiple variables -
this seems basic question know i've not been able find answer.
let's assume basic function:
function basicfunction ( $var1, $var2 = 1, $var3 = 2, $var4 = 5 ) { // stuff // return } now let's assume want call function following variables:
$var1 = 0 $var2 = 1 $var3 = 2 $var4 = 3 i can this:
$someresult = basicfunction( 0, 1, 2, 3 ); $var2 , $var3 set though, how call function without having repeat value $var2 , $var3?
php not support overloading. therefore, cannot skip them in way if don't move them right of list of arguments.
a common solution set default value of different type expected (i.e. null). actual default value set within function. approach not clean , takes lines of code, if situation requires it, can go this:
function basicfunction($var1, $var2 = null, $var3 = null, $var4 = null) { if ($var2 === null) { $var2 = 1; } // ...
Comments
Post a Comment