This seems a basic question I know but I’ve not been able to find the answer.
Let’s assume a basic function:
function basicFunction ( $var1, $var2 = 1, $var3 = 2, $var4 = 5 )
{
// Do some stuff
// Return
}
Now let’s assume I want to call that function with the following variables:
$var1 = 0
$var2 = 1
$var3 = 2
$var4 = 3
I can do this:
$someResult = basicFunction( 0, 1, 2, 3 );
$var2 and $var3 are already set though, so how would I call the function without having to repeat the value for $var2 and $var3?
PHP does not support overloading. Therefore, you cannot skip them in any way if you don’t move them to the very right of the list of arguments.
A common solution is to set a default value of a different type than expected (i.e. NULL). The actual default value is then set within the function. This approach is not really clean and takes some extra lines of code, but if the situation requires it, you can go with this: