How would I skip optional arguments in a function call?

Sebastian picture Sebastian · Jul 1, 2009 · Viewed 61.7k times · Source

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.

Answer

zombat picture zombat · Jul 1, 2009

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 '' or null, and don't use them inside the function if they are that default value.