0

I read somewhere that when you define a function you can define a default value for a variable that you might use or not use, like so:

function test( $a, $b, $c=0 ) {
    if ( $c != 0 )
        echo 'a + b + c = '.( $a + $b + $c);
    else
        echo 'a + b = '.( $a + $b );
}

I was wondering how many such variables with default values I can add when I define a function. Is it only one? or can I define multiple ones without getting a "function expects X variables to be passed" type of error? In other words can I do this?

function test( $a, $b, $c=0, $d=1, $e=2 ) {
    if ( $c != 0 )
        echo 'a + b + c = '.( $a + $b + $c);
    else
        echo 'a + b = '.( $a + $b );
}

and only call the function with test(4,9); ?

6
  • 5
    You can have as many as you want. Commented Dec 24, 2013 at 17:49
  • 3
    Just test it with any PHP installation. (Hint: you won't get any errors) Commented Dec 24, 2013 at 17:51
  • 2
    When you can test something out faster than you can type out a long question on it... test it first :) Commented Dec 24, 2013 at 17:51
  • 1
    Or read the documentation. The second example at php.net/manual/en/… shows two arguments with default values. Commented Dec 24, 2013 at 17:53
  • @Vulcan I know I can test it and I did but I typed out the question because I once read somewhere that you can only have one and so I wanted to know what the deal was and if there were any counter-indications to using multiple... Commented Dec 24, 2013 at 18:12

3 Answers 3

1

You can define as many optional parameters as you want, PHP offers this in place of method overloading like you would see in Java or C#.

Make sure you don't put any non-optional parameters after the optional ones. It may seem like common sense, but I have seen people get confused because they receive a warning or error when trying to do this:

Invalid:

function myFunction($a = 1, $b = 2, $c, $d = null)

In the above case, $c would need to be the first argument.

Sign up to request clarification or add additional context in comments.

1 Comment

Just remember that mandatory arguments come first, then optional. And, optional arguments are assigned from left to right, so you can't skip over any and use a "later" one.
1

This is one of the function OVER LOADING , The following example will clarify your doubt,

 class argument_class
{
  function mymethod($arg1 = null, $arg2 = null, $arg3 = null)  
  {  
     if( $arg1 == null && $arg2 == null && $arg3 == null ){ 
     return 'function has got zero parameters <br />';
  }
  else{
  $str = '';
  if( $arg1 != null ) 
     $str .= "arg1 = ".$arg1." <br />";
  if( $arg2 != null ) 
      $str .= "arg2 = ".$arg2." <br />";
  if( $arg3 != null ) 
     $str .= "arg3 = ".$arg3." <br />";
    return $str;
    }
  }
}



 $obj = new argument_class;

 echo '<br />$obj->mymethod()<br />';
 echo $obj->mymethod();

 echo '<br />$obj->mymethod(null,"argument_2") <br />';
 echo $obj->mymethod(null,'argument');

 echo '<br /> $obj->mymethod("argument_1","argument_2","argument_3")<br />';
 echo $obj->mymethod('argument_1','argument_2','argument_2');

Comments

1

You ask about function arguments with default values. PHP does not apply a limit here, it is merely bound to available memory.

As I have showed in an answer to a related question (PHP Functions - Maximum number of arguments), it was possible to have 255 555 arguments before running into a memory limit Sure this is a single case, this can vary depending on available memory.

That question also contains another answer that explains what happens when you call a function. Understanding that should also help you to make your decisions about number of parameters better.

From a coding standpoint, a function should have as little parameters as possible, at best 0, perhaps one but any more parameters should be prevent as best as possible. If you're not able to reduce the number of parameters, this is a sign to review the design of the code.

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.