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.

Peter
  • 6,147
  • 7
  • 25
  • 15
  • 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. :) – edwardtyl Jul 22 '15 at 18:51

6 Answers6

55

try this:

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

then you can call

test($required, $optional)

and with $optional null

test($required);  
felixsigl
  • 2,900
  • 1
  • 23
  • 16
  • You mean when I pass `$required` AND '$optional' with values, this `$optional=NULL` would be overwritten? will work perfectly with the `$optinal` data? – Neocortex Dec 16 '14 at 06:32
11

With a default value:

function test($required, $optional="default value"){..}
Skilldrick
  • 69,215
  • 34
  • 177
  • 229
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

Alp
  • 29,274
  • 27
  • 120
  • 198
6

You can also do this:

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

when calling:

test($required)

or:

test($required, $optional = 'something')
Limon
  • 1,772
  • 7
  • 32
  • 61
  • 2
    calling with `test($required, $optional = 'something')` is misleading, you're assigning `$optional` variable, not passing optional argument value by name – McX Mar 30 '18 at 09:50
  • @McX does it work? – Limon Mar 30 '18 at 15:03
  • 1
    @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 Apr 02 '18 at 07:22
  • @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. – Limon Apr 03 '18 at 12:00
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);
mjspier
  • 6,386
  • 5
  • 33
  • 43
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");
?>
xkeshav
  • 53,360
  • 44
  • 177
  • 245