No, unfortunately you can't "step over" arguments in PHP, so default values are only possible at the end of the list.
So you can write this:
function foo($a=0, $b=0) {}
foo(42);
Here $b
will have its default value but $a
won't, as we provided one value as input.
However there's no way of providing a value for $b
without also providing one for $a
- we have to provide the value for $b
as the second parameter, and PHP has no keyword we can use in place of the first parameter to say "use default".
There is talk of adding named parameters to a future version of PHP, similar to your non-PHP example.
You can simulate this a bit with some changes to your code, though; a couple of ideas:
- treat
null
as meaning "use default", and write$a = is_null($a) ? 42 : $a
- make your functions take an associative array as their only parameter, and take values from it as though their keys were parameter names