Is it possible to use an array as a default parameter in a PHP function?
I would like to do the following:
$arr = array(1,2,3,4);
function sample($var1, $var2, $var3 = $arr){
echo $var1.$var2;
print_r($var3);
}
sample('a','b');
// Should still work without the third parameter and default to $arr
No, this is not possible, the right hand expression of the default value must be a constant or array literal, i.e.
If you want this behaviour, you could use a closure:
You could also express it with a class: