How would I skip optional arguments in a function call?
You are encountering a common misconception about how PHP handles default values and argument passing. The short answer is: you cannot "skip" an argument to make it use its default value while simultaneously providing a subsequent value. In the language's execution model, positional arguments are strictly ordered. When you write getData('some name', '', '23'), PHP binds every provided argument to the corresponding parameter in function definition order, regardless of whether that argument is empty or matches the default string.
In your specific example:
function getData($name, $limit = '50', $page = '1') {
// ...
}
getData('some name', '', '23');
The call getData('some name', '', '23') results in $name receiving 'some name', $limit receiving the empty string '', and $page receiving '23'. The function does not look at the value of $limit to decide whether to use the default; it simply takes whatever was passed in that specific slot. If your intention is to have $limit be '50' (the default) while setting $page to '23', passing an empty string for the middle argument will not achieve that outcome.
To pass a value to the third parameter while leaving the second parameter untouched, you must use named arguments. This feature allows you to explicitly map values to parameters by name, effectively bypassing the positional constraints. You can call the function as follows:
getData(name: 'some name', page: '23');
In this invocation, PHP matches 'some name' to $name and '23' to $page. Because $limit is not specified in the call, it automatically defaults to '50'. This approach works reliably in PHP 8.0 and later. If you are working with an older version of PHP (7.x or earlier), named arguments are not supported, and you would be forced to pass null explicitly if you wanted to skip a parameter, though that only works if the function definition accepts null as a valid default or if you handle null internally.
// PHP 8.0+ syntax
getData(name: 'some name', page: '23');
// PHP 7.x workaround (requires default value of null)
function getData($name, $limit = null, $page = '1') {
$limit = $limit ?? '50'; // Set default inside function if not passed
}
getData('some name', null, '23');
When using named arguments, the order of parameters in the function definition no longer dictates the order of the call. This flexibility eliminates the risk of passing null or an empty string by accident to a parameter that expects a specific type. It also makes the code significantly more readable, as the intent is clear from the variable names rather than relying on position alone.
However, this convenience comes with a trade-off: you must ensure your function signature supports named arguments if you intend to use them in production codebases targeting older PHP versions. Additionally, mixing positional and named arguments in a single call can lead to confusing errors if not done carefully. For instance, providing a positional argument followed by a named one for the same parameter is invalid syntax:
getData('some name', '50', page: '23'); // SyntaxError: Parameter $page passed by value and named in the same call
If you find yourself frequently needing to skip parameters, consider refactoring your function to group related data or use a DTO (Data Transfer Object) to encapsulate the arguments, which can reduce the cognitive load of managing multiple optional flags.