Is it possible in PHP to specify a named optional parameter when calling a function/method, skipping the ones you don’t want to specify (like in python)?
Something like:
function foo($a, $b = '', $c = '') {
// whatever
}
foo("hello", $c="bar"); // we want $b as the default, but specify $c
No, it is not possible (before PHP 8.0): if you want to pass the third parameter, you have to pass the second one. And named parameters are not possible either.
A “solution” would be to use only one parameter, an array, and always pass it… But don’t always define everything in it.
For instance :
And calling it this way : (Key / value array)
Will get you this output :
But I don’t really like this solution :
So I’d go with this only in very specific cases — for functions with lots of optional parameters, for instance…