Any way to specify optional parameter values in PHP?

20,904

Solution 1

PHP does not support named parameters for functions per se. However, there are some ways to get around this:

  1. Use an array as the only argument for the function. Then you can pull values from the array. This allows for using named arguments in the array.
  2. If you want to allow optional number of arguments depending on context, then you can use func_num_args and func_get_args rather than specifying the valid parameters in the function definition. Then based on number of arguments, string lengths, etc you can determine what to do.
  3. Pass a null value to any argument you don't want to specify. Not really getting around it, but it works.
  4. If you're working in an object context, then you can use the magic method __call() to handle these types of requests so that you can route to private methods based on what arguments have been passed.

Solution 2

A variation on the array technique that allows for easier setting of default values:

function foo($arguments) {
  $defaults = array(
    'firstName' => 'john',
    'lastName' => 'doe',
  );

  $arguments = array_merge($defaults, $arguments);

  echo $arguments['firstName'] . ' ' . $arguments['lastName'];
}

Usage:

foo(array('lastName' => 'smith')); // output: john smith

Solution 3

You could refactor your code slightly:

function foo($firstName = NULL, $lastName = NULL)
{
    if (is_null($firstName))
    {
        $firstName = 'john';
    }
    if (is_null($lastName ))
    {
        $lastName = 'doe';
    }

    echo $firstName . " " . $lastName;
}

foo(); // john doe
foo('bill'); // bill doe
foo(NULL,'smith'); // john smith
foo('bill','smith'); // bill smith

Solution 4

If you have multiple optional parameters, one solution is to pass a single parameter that is a hash-array:

function foo(array $params = array()) {
    $firstName = array_key_exists("firstName", $params) ?
      $params["firstName"] : "";
    $lastName = array_key_exists("lastName", $params) ?
      $params["lastName"] : "";
    echo $firstName . " " . $lastName;
}

foo(['lastName'=>'smith']);

Of course in this solution there's no validation that the fields of the hash array are present, or spelled correctly. It's all up to you to validate.

Solution 5

The way you want: no.

You could use some special mark, like NULL to note that value is not supplied:

function foo($firstName, $lastName = 'doe') {
    if (is_null($firstName))
        $firstName = 'john';
    echo $firstName . " " . $lastName;
}

foo(null, 'smith');
Share:
20,904
zaczap
Author by

zaczap

i keep learning new things

Updated on August 05, 2022

Comments

  • zaczap
    zaczap almost 2 years

    Let's say I've got a PHP function foo:

    function foo($firstName = 'john', $lastName = 'doe') {
        echo $firstName . " " . $lastName;
    }
    // foo(); --> john doe
    

    Is there any way to specify only the second optional parameter?

    Example:

    foo($lastName='smith'); // output: john smith