Default Function Parameters In PHP

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.

function testFunction($a = 1)
{
    return $a;
}

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.

echo testFunction(); // prints 1
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.

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

This function can be run in the following manner.

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

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

function testArrayFunction($array = array('someKey'))
{
    if (isset($array['someKey'])) {
        return $array['somekey'];
    }
}

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.

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

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.

function testFunction($a, $print = null)
{
    // Increment $a
    ++$a;
    if ($print === true) {
        // Print $a
        echo $a;
    }
    // Return $a
    return $a;
}

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

testFunction(2, true); // prints and returns 3
testFunction(2); // returns 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.

$a = 1;
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.

$a = 1;
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.

Comments

Keep posting stuff like this i really like it
Permalink
Great coding!
Permalink

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

Permalink

That was very useful. Thanks !

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
3 + 10 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.