Writing Functions In PHP – Default Values

Writing Functions in PHP – Introduction

Writing Functions in PHP – Flexibility

Writing Functions in PHP – Return Values

Writing Functions in PHP – Global Variables

We saw earlier how the numbers of arguments that were passed into a function needed to match the number of arguments that the function was defined with. But with default arguments, that’s not entirely true because we can set some defaults so that if it doesn’t get a value, it has something that it can assume, and this not only will make our functions more flexible but also error resistant. Let’s go ahead and try one now…

<html>
<head>
<title>Writing Functions In PHP - Default Values</title>
</head>
<body>
    <?php

        function paint($color) {
            echo &quot;The color of the room is {$color}.&quot;;
        }

        paint(&quot;blue&quot;);

    ?>
</body>
</html>

Output: the color of the room is blue.

That is a simple straight forward example. What if we didn’t pass a value into the function?

    <?php

        function paint($color) {
            echo &quot;The color of the room is {$color}.&quot;;
        }

        paint();

    ?>

Sure we will get an error message.

Output:
Warning: Missing argument 1 for paint()…
Notice: Undefined variable…
The color of the room is .

Functions With Default Values

In the example above, we can set a default value for $color, so that if it doesn’t get a $color, we have something to fall back on. We do that by simply putting in a normal equal assignment in the function right after the arguments.

    <?php

        function paint($color=&quot;red&quot;) {
            echo &quot;The color of the room is {$color}.&quot;;
        }

        paint();

    ?>

So now, “red” will be our default value if we haven’t passed any value to the function. In other words, if no value were passed into the function, use “red”! The default value is overwritten when you specify a value, otherwise the value will stay the same. Even though we didn’t ask for another argument, it went ahead and assume “red”. If instead we tell it “blue”, it goes ahead and uses “blue”.

So you can see how this is going to make our function a little more error resistant because it will not give us that big nasty error if we didn’t pass something in.

Flexible Functions With Default Values

Let’s go ahead and make this function even more flexible. Lets say we just are not going to paint the “room”, but instead we are going to paint the $room as a variable.

<html>
<head>
<title>Writing Functions In PHP - Default Values</title>
</head>
<body>
    <?php

        function paint($room=&quot;office&quot;,$color=&quot;red&quot;) {
            echo &quot;The color of the {$room} is {$color}.&quot;;
        }

        paint(&quot;bedroom&quot;,&quot;blue&quot;);

    ?>
</body>
</html>

Output: The color of the bedroom is blue.

So, this function takes two arguments into it and we must pass two values into it as well. Even though we didn’t pass anything into the function, it went ahead and made the assumption with the default value that we have declared earlier. All of this will work:

<?php paint(); ?>

Output: The color of the office is red.

<?php paint(&quot;bedroom&quot;); ?>

Output: The color of the bedroom is red.

What if we say “blue” and leave out the other one which is the $room?

<?php paint(&quot;blue&quot;); ?>

Output: The color of the blue is red.

It went ahead and says “The color of the blue is red.”, and why is that? This is because it’s going to still assume that the arguments are coming in the correct order. So we have to make sure that we always pass something in the right order.

Required Value vs Not Required value

Consider this example:

    <?php

        function paint($room,$color=&quot;red&quot;) {
            echo &quot;The color of the {$room} is {$color}.&quot;;
        }

        paint(&quot;bedroom&quot;,&quot;blue&quot;);

    ?>

Output: The color of the bedroom is blue.

We know this will work. $room is a required value and $color is not a required value, in this function. This means, a required value is a value that is having the highest priority for us, we want to make sure that we pass the correct value to this required value. Not like the $color value, which is not a required value, we can choose whether we want to specify the value or just let the function go ahead and assume for us.

If we did it the other way around and made our $color as the required value and pass in only one argument into the function:

    <?php

        function paint($room=&quot;office&quot;,$color) {
            echo &quot;The color of the {$room} is {$color}.&quot;;
        }

        paint(&quot;bedroom&quot;);

    ?>

Output:
Warning: Missing argument 2 for paint3()…
Notice: Undefined variable…
The color of the bedroom is.

This function will take “bedroom” as being the $room, and it’s going to ask for the required value which is the $color. We didn’t pass any value to the function for the variable $color. This will return an error message to us.

So you want to always make sure that your default values occur last in line. In other words, anything that is required needs to come first in the arguments list, anything that is optional needs to come later and the order of this doesn’t matter and you can’t skip spaces in between. So you need to pass in something along the way.

Conclusion

Another thing that I found helpful with default arguments is that you very quickly get a sense of what kind of information you’re expecting to be passed in to your function.

<?php function paint($color=&quot;red&quot;,$room=&quot;office&quot;) {?>

Clearly here, I am looking for a color, I know it is going to be “red” and I am looking for a room which I know is something like an “office”. This gives us some context for knowing what those variables actually mean in our function.

Now that we’ve looked into default argument values, I think we’ve explored enough of functions that we will be able to get a lot out of them once we start coding and developing.

Thank you and enjoy learning.

One Comment on "Writing Functions In PHP – Default Values"

  1. David Hayes says:

    Very nice, you told me exactly what I needed to know.

    Also, your & are html escaped in your ".

Got something to say? Go for it!