This is an old revision of the document!

Skipping optional parameters for functions


As PHP does not have named parameter support, a very common for function is to have many optional arguments, like this:

   function create_query($where, $order_by, $join_type='', $execute = false, $report_errors = true)

If we always use defaults, it's fine. But what if we need ot change $report_errors but don't care about the others? We'd have to find function definition and copy-paste all other defaults into the call, which is annoying, error-prone and may not do what you wanted if some of the defaults change.


The proposal is to allow skipping optional arguments in a call, thus making them assume default values as they do when they are not provided, like this:

    create_query("deleted=0", "name", default, default, /*report_errors*/ true);

This means that $join_type and $execute are going to use defaults. Of course, if we ever get implementation of named parameters, it may also solve this problem, but until we do, this can be a partial solution.

Only optional parameters can be skipped this way, skipping non-optional one will produce the same error it does now when function is not given enough parameters.


On the engine level, it will be implemented by putting NULL in the place where the parameter is passed. Functions dealing with argument handling will be updated.

See example implementation above. See tests for examples of most common uses cases.

User functions

User functions would have defaults for optional args take place of skipped arguments, just as before.

Internal functions

For internal functions, parameter parser will ignore the NULLs, thus leaving the defaults supplied by the caller intact. Again, skipping non-optional parameter is an error.

Variadic functions will not return skipped parameters in argc and argv, effectively ignoring them, so:


is the same as:



func_get_args() will skip parameters that are not supplied, so it would provide just the parameters that are actually supplied to the function. Thus, this code:

   function test($a=1, $b=2, $c=3) {

will produce:

    array(2) {

This means that if you are looping over func_get_args() result, you should use foreach() and not counter-based for(). func_num_args() will also ignore skipped parameters and give only the number of parameters actually passed. func_get_arg() will return false for parameter that was not passed.


call_user_func_array() will allow skipping arguments too by skipping them in supplied arguments array, like this:

    call_user_func_array('test', array(42, 2=>43, 4=>44));

call_user_func will allow argument skipping directly:

    call_user_func('test', 42, default, 44);

with the same result.

Internal API changes

Parameters stored as zval** in the engine. For skipped parameter, the value stored is NULL instead of zval* to specific argument. The opcode putting NULL on the stack is implemented as znode type IS_UNUSED.

zend_parse_parameter() would ignore parameters marked as default - meaning, it will not assign any value to the underlying variable. This is unless the parameter is marked as !, in which case the parameter would be nullified just as if null were passed. This means you can not have variables marked as ! with different behavior between null and 'default', but I did not find any such cases to be required.

For variadic parameters like + and *, skipped parameters will be ignored and not included in the list as if they were not passed. In other words, + and * will never return NULLs among parameters.

If certain function wants to disallow skipping parameters, it should use option ZEND_PARSE_PARAMS_NODEFAULT with zend_parse_parameters_ex(). In this case, skipped parameters will cause . For older APIs, zend_get_parameters_array_nodefault() is available which will fail with E_WARNING “Parameter %d missing” if any skipped parameters are passed (i.e. would never return NULLs as parameters).

ZEND_NUM_ARGS() is always the number of parameters in function call, so skipped parameters are counted there. But trying to get a skipped parameter with API functions will produce NULL.

User request examples

Issues raised

  • Support for internal functions doing manual ZEND_NUM_ARGS(). I've looked into all functions in the standard distribution, but PECL extensions that are not using zend_parse_parameters or do weird things with zval-type ones without properly initializing them or checking the output results, may require fixing, otherwise they may crash if “default” is passed to them due to null pointer reference. This crash doesn't seem to be dangerous security-wise, but would of course be unpleasant.


  • 2012-04-13 First draft.
  • 2012-07-07 Changed empty parameter to use 'default'
  • Added Zend API description
rfc/skipparams.1378158120.txt.gz · Last modified: 2017/09/22 13:28 (external edit)