Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to specify optional parameter values in PHP?

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
like image 961
zaczap Avatar asked Mar 27 '09 17:03

zaczap


People also ask

How do you specify an optional parameter?

Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list aren't supported.

Are parameters optional in PHP?

When creating functions in PHP it is possible to provide default parameters so that when a parameter is not passed to the function it is still available within the function with a pre-defined value. These default values can also be called optional parameters because they don't need to be passed to the function.

Can you assign the default values to a function parameters in PHP?

PHP allows us to set default argument values for function parameters. If we do not pass any argument for a parameter with default value then PHP will use the default set value for this parameter in the function call.


3 Answers

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.
like image 124
Noah Goodrich Avatar answered Sep 27 '22 23:09

Noah Goodrich


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 
like image 35
ceejayoz Avatar answered Sep 27 '22 23:09

ceejayoz


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
like image 38
Andrew G. Johnson Avatar answered Sep 27 '22 22:09

Andrew G. Johnson