Default Function Parameters In PHP

26th January 2010 - 6 minutes read time

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. I have seen this sort of code being used incorrectly quite often recently so I thought I would go over it in a post.

Creating a default parameter in a function is very simple and is quite like normal variable assignment. The following function has a single parameter that is set to 1 if it is not passed when calling the function.

  1. function testFunction($a = 1)
  2. {
  3. return $a;
  4. }

This function doesn't do very much, but can be used in the following way. If the parameter isn't added to the function call then the default value is used, if it is added then this value is used instead.

  1. echo testFunction(); // prints 1
  2. echo testFunction(2); // prints 2

It is possible to use as many default arguments as is needed in a function. The following function has four parameters, three of which are optional.

  1. function testFunction($a, $b = 1, $c = 1, $d = 1)
  2. {
  3. return $a + $b + $c + $d;
  4. }

This function can be run in the following manner.

  1. echo testFunction(1); // prints 4
  2. echo testFunction(1, 2); // prints 5

It is also possible to use arrays as default parameters as in the following function.

  1. function testArrayFunction($array = array('someKey'))
  2. {
  3. if (isset($array['someKey'])) {
  4. return $array['somekey'];
  5. }
  6. }

One important thing to remember when creating default parameters is that you should add them to the end of the required parameters that your function has. It is syntactically valid to write a function in which the default parameters are not at the end of function definition, but this will lead to problems. Take the following function.

  1. function testMultiFunction($a, $b = 1, $c, $d = 1)
  2. {
  3. return $a + $b + $c + $d;
  4. }

It is impossible to actually use any default parameters that are set before any non-default parameters. In the case of the code above $b will never be available as the default value of 1. PHP has no way of knowing that you want to skip a particular parameter and will therefore assign them as it always will do, in the order they are passed. Trying to skip out one of the parameters will lead to a syntax error.

echo testFunction(1, , 1); // syntax error

Just in case some of you are thinking that it should be possible to simply add a null value as the parameter you should note that this will not work either. What you are essentially doing is passing a value (null in this case) and so the default parameter will not be used. Using the above function the following code will print 3. This is the result of the sum 1 + null + 1 + 1, the null value translates to 0 when used in a calculation.

echo testFunction(1, null, 1); // prints 3

Default parameters can be used to control the function in some way. Lets say that you wanted to create a function that will print out a variable if the function is told to do so. To do this you would create a second optional parameter that would print a given value if true is passed.

  1. function testFunction($a, $print = null)
  2. {
  3. // Increment $a
  4. ++$a;
  5. if ($print === true) {
  6. // Print $a
  7. echo $a;
  8. }
  9. // Return $a
  10. return $a;
  11. }

This function style can be seen in many CMS systems, usually within template functions, and can be run in the following way.

  1. testFunction(2, true); // prints and returns 3
  2. testFunction(2); // returns 3
  3. testFunction(2, false); // returns 3

The mistake I have seen the most often is when programmers copy and paste the function definition and use this as the function call itself. The following (perfectly legal) code calls the testMultiFunction() example function above.

  1. $a = 1;
  2. echo testMultiFunction($a, $b = 1, $c = 1, $d = 1); // prints 4

What we are doing here is defining the variables $b, $c and $d to be 1, which are then passed to the function in the form of parameters. In PHP it is possible to run code within function calls, the outcome of which is then passed to the function. However, this is a little wasteful as all we are doing here (and what usually happens) is defining variables that never get used after this function call. To tidy up this code just remove the variable definitions and keep the values that you want to pass to the function.

  1. $a = 1;
  2. echo testMultiFunction($a, 1, 1, 1); // prints 4

This has the same output as the previous code, but it is a much cleaner way to call the function. If you are worried about not being able to tell what parameter does what then add a comment before the function call that gives you more information.


Keep posting stuff like this i really like it

forex robot (Wed, 02/10/2010 - 04:41)

Great coding!

sarah (Mon, 01/03/2011 - 19:57)


Just learning PHP (experienced with another programming language though) and this article really helped me grasp the way PHP handles params. Thanks!

Tom (Fri, 06/15/2018 - 00:28)


That was very useful. Thanks !

Anonymous (Mon, 05/04/2020 - 06:30)

Add new comment

The content of this field is kept private and will not be shown publicly.