OK I totally forgot how to skip arguments in PHP.
Lets say I have:
function getData($name, $limit = '50', $page = '1') {
...
}
How would I call this function so that the middle parameter takes the default value (ie. '50')?
getData('some name', '', '23');
Would the above be correct? I can't seem to get this to work.
Your post is correct.
Unfortunately, if you need to use an optional parameter at the very end of the parameter list, you have to specify everything up until that last parameter. Generally if you want to mix-and-match, you give them default values of
''
ornull
, and don't use them inside the function if they are that default value.Nope, it's not possible to skip arguments this way. You can omit passing arguments only if they are at the end of the parameter list.
There was an official proposal for this: https://wiki.php.net/rfc/skipparams, which got declined. The proposal page links to other SO questions on this topic.
This snippet:
Answer is :
Try This.
This is what I would do:
Hope this will help someone
For any parameter skipped (you have to) go with the default parameter, to be on the safe side.
(Settling for null where the default parameter is '' or similar or vice versa will get you into troublew...)