2

Possible Duplicate:
How do you create optional arguments in php?

I built a function with 1 argument. That function is called numerous places on many, many pages on my site. I just decided that I need another parameter for this function.

In order to avoid "Warning: Missing argument" errors, is there any way to add a second argument with a bit of logic that tests the presence of a second argument in the calling command?

I do not want to go back through my entire website and try to catch and update every function call merely to throw a null value on it in order to void the error messages.

Thanks.

Community
  • 1
  • 1
H. Ferrence
  • 7,906
  • 31
  • 98
  • 161

3 Answers3

7

You can specify a default value for your arguments:

function foo($arg1, $optional_arg2 = NULL)
{
  // ...
}

$optional_arg2 = NULL specifies that if not given when the function is called, $optional_arg2 will be NULL. The default value of an argument can be a scalar value (string, number or boolean), an array, or NULL, but it cannot be a variable, class member or function call.

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
1

You can define default the second argument value.

function functionName( $first, $second = false ) { ... }

And now if you left the second argument it will have false value. In the next case you call

functionName("test", true);

and overwrite default value.

hsz
  • 148,279
  • 62
  • 259
  • 315
0
//@params array $options

    function test($options) {
       $option1 = $options['option1'];
       ...
    }
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143