33

How can I pass an optional parameter to a PHP function. For example

function test($required, $optional){..}

So that i should be able to call the function by passing either one parameter or both. For example:

test($required, $optional)
test($required);

Thanks.

1
  • wow almost every single one of the answers for this question say the exact same thing. I don't know how you managed to pick one because they're all correct. :) Commented Jul 22, 2015 at 18:51

6 Answers 6

56

try this:

function test($required, $optional = NULL){..} 

then you can call

test($required, $optional)

and with $optional null

test($required);  
Sign up to request clarification or add additional context in comments.

1 Comment

You mean when I pass $required AND '$optional' with values, this $optional=NULL would be overwritten? will work perfectly with the $optinal data?
11

With a default value:

function test($required, $optional="default value"){..}

Comments

8

1) You can define default values for arguments like so:

function test($required, $optional = false) {}

2) You can use func_num_args() to get the number of arguments, func_get_arg($n) to get the n'th argument or func_get_args() to get an array of all arguments.

You can find a good summary here: Function arguments

2 Comments

+1 for mentioning another way
yes +1 for mentioning another way. It sure beats a repeat answer for an entire page.
6

You can also do this:

function test($required, $optional = ''){
    #code
}

when calling:

test($required)

or:

test($required, $optional = 'something')

4 Comments

calling with test($required, $optional = 'something') is misleading, you're assigning $optional variable, not passing optional argument value by name
@McX does it work?
@Lemon It works with a side effect which if not well understood can lead to errors. For example someone may try to call function f($a='', $b='') with f($b='b') and think it will call f('', 'b') when it will actually call f('b', '') and set the global variable $b='b'
@McX that's a good point, but I guess it has more to do with being able to pay attention to the function you want to call. That can be solved by just adding 1 line of comment, preventing this unwanted side effect.
2

just set the default parameters in your function ex.

function test($required, $optional = null, ... )

now you can call your function like this

test($required, $optional)
test($required);

Comments

0

read this http://php.net/manual/en/function.func-get-args.php

dont pass any argument when decalare a function like below

function myMin() {
      $list = func_get_args();
      $min = NULL;
      if (count($list)>0) $min = $list[0];
      for ($i=1; $i<count($list); $i++) {
         if ($min > $list[$i]) $min = $list[$i];
      }
      return $min;
   }

   print("\n Calling a function that uses func_get_args():\n");
   print("    ".myMin()."\n");
   print("    ".myMin(5)."\n");
   print("    ".myMin(5, 6, 2)."\n");
   print("    ".myMin(5, 6, 2, 9, 5, 1, 4, 1, 3)."\n");
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.