Sign In | Subscribe

Enter your Sign on user name and password.

Forgot password?
  • Follow us on:
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Optional Parameters

  • User-defined functions can be defined to allow for optional parameters . In order to define a parameter as optional, it must be assigned a default value in its function's definition.
  • Default values must be constant values and can be of the following data types:
    • Scalar (int, float, string, bool)
    • Array
    • NULL
  • If a function has multiple optional parameters, not all optional parameters need to be supplied with argument values when calling the function.
  • For any optional parameter you wish to provide a value, you must also provide values for all ‘prior’ optional parameters defined in the function’s parameter list. Any ‘later’ optional parameters do not have to be provided values.
  • Additional Resources:

Optional Parameters

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:10
    • Lesson Overview
  • Optional Parameters 0:26
    • Optional Parameters Definition
    • Default Values
    • Optional Parameters Coding Example
  • More on Optional Parameters 6:55
    • Multiple Optional Parameters
    • Coding Example
  • Homework Challenge 16:18
    • Homework Challenge

Transcription: Optional Parameters

Hello again, and welcome back to's Introduction to PHP course.0000

In today's lesson, we are going to be introducing the topic of optional function parameters.0004

Specifically, we are going to be talking about optional function parameters, describing what they are0011

and how they are defined within a function, as well as how to call functions that have optional parameters as an option.0017

As mentioned, when we define user-defined functions in PHP, they can have optional parameters.0030

And what that means is: the user can choose, when they call that function, whether to provide an argument for a particular optional parameter.0036

All of the functions we have defined so far have required parameters, which means,0044

when you call the function, if it has two parameters that it takes, you have to provide two arguments to that function.0047

The way you define an optional parameter is like this.0054

Here, we have a function, just called test, and an optional parameter is set up define the name that you want to use0058

for the optional parameter, and then you give it a default value.0067

When you define a function in this way, any parameters that are defined without default values,0071

such as req for required parameter in this case, are going to be required parameters.0078

Any ones that you define with default values are going to be optional parameters.0083

Now, what that means is that, when you are accessing (for example), within the body of your function, these different variables,0090

these parameter variables, when you access a required variable, you will just access it as usual.0098

When you access...for example, let's say we want to output the optional parameter that a user passed in...0105

If the user passed in an optional parameter, then optional is going to be set to that value.0110

So, if the user called this function, for example, then required is going to be set to 5, and the optional parameter is going to be set to value 5, as well.0116

If we had just called test with one parameter (in this case, 5), then required is going to be set to 5.0133

And then, any time, for example, let's say we want to echo the optional parameter in our function--any time optional is accessed--0141

in this case, because we didn't provide it with a value, the default value of 1 is going to be used.0155

So, in this case, it would output the value 1, because on this call to test, we did not provide the optional parameter.0160

One thing to note is that the default value could be set (I'll clean this up a little bit) within the function definition.0168

These values must be constant values, and they also must be of the following data types.0178

They can be scalar constant values, which can be constants or actual literal values (such as, for example, the literal number 1).0184

They can be any of the scalar types; they can be an array, as long as the array is filled with literal values;0194

or the array may have constant values in it, as well.0200

Or, they can be set to the default value null.0203

Let's take a look at a script here called optionalParameters.php.0210

Here, we are going to define a function called optional that is going to have two parameters.0215

It is going to have on that is a required parameter and one that is an optional parameter.0220

The required parameter is going to be called required; the optional parameter is going to be named optional.0224

And so, what this function does, essentially, is just outputs the two values0229

of the required parameter and the optional parameter that may have been passed to the function.0234

What I am going to do is go ahead and fill in here the function definition that shows how optional parameters are created.0239

With required, we don't set a default value, because it is a required parameter.0245

However, with our second parameter, named optional--it is an optional parameter, so we have to give it a default value0249

in order to signify that it is an optional parameter.0258

In this case, we are going to set the default value for optional equal to 1.0267

And then, down here, in this section, I have a little message saying that we are only providing the required argument.0271

So then, now that we have defined this function, if we go ahead and call optional and provide it (for example) with the required parameter0276

(we'll just pass in the value of 5), what is going to happen is: required is going to be set to 5, and optional,0287

because no value was passed in, is going to be set to 1.0294

So, if we save this and go ahead and view it in our browser, we can see that required is set to 5; optional is set to 1.0297

And that is because optional was not provided when we called the function.0305

Now, if we go back here, I have a second statement that says we are going to provide both the required and optional parameters.0311

We call optional again; in this case, we provide the same required parameter; but this time, let's say, we provide the value 12 for the optional parameter.0318

When we run the function, required is going to get set to 5; and then optional, instead of being equal to 1, is going to get set to 12.0326

So, we should have the output that shows 5 and 12.0332

And if we save this and go ahead and reload the page, we can see here, in this second statement,0336

that required was set to 5, and the optional value was actually set to 12.0343

And as mentioned, these parameter default values for optional parameters must be constant functions.0351

So, you could include a constant in here; there is a constant we haven't defined.0360

But if you try to do something like (for example) make a function call--for example, to time,0366

what is going to happen is: you are going to get a parse error when you try to run the code.0374

If we reload the page, it is going to say "unexpected parentheses," and what that is telling you is that you can have only constant values0379

and nothing that does any sort of function calls or computations, or anything like that.0392

Alternatively, we could have a value null, as we had mentioned.0397

In this case, if we go ahead and refresh our script, we can see that optional here outputs nothing, because the string value of null is the empty string.0402

There are a couple more things to mention about optional parameters, and they have to do with0417

when functions have multiple optional parameters, which is completely allowed.0421

You can have as many optional parameters to a function as you require.0426

And the thing to note is that when you call a function requiring optional parameters, arguments do not need to be provided for all of the optional parameters.0430

That is why they are called optional parameters.0437

However, there are some rules that you need to follow when calling functions with optional parameters.0440

First of all, if (let's say) a function takes one required parameter and three optional parameters, if you want to only provide0447

a value for that third optional parameter, there is a special way that you have to call the function, which we are going to show in a second.0456

And what it does is says that, for any other optional parameters that are previously defined in the function, you have to provide them at least some value.0463

And then, let's say for a function that has three optional parameters and one required parameter, you only want to provide the first optional parameter.0473

Well, you can actually leave off the second two without any problems.0482

Let's go take a look at another script called moreOptionalParameters.php.0486

Here, we have a function that is going to accept one required parameter and three optional parameters.0491

I'll put these on separate lines to make it look neater.0501

And I am just going to set these to some string default values.0511

This right here is our parameter list for this function; it contains one required parameter and three optional parameters,0531

which you can identify by the equal sign that is assigning a default value to those optional parameters.0543

So, if we call this function with just the required parameter, it is going to output the value of the required parameter that we passed in,0552

and then output these three default values.0559

If we go ahead and call this function with just the required parameter--just call it required value as a string--and we go ahead and0563

load this page in our browser, you can see that it outputs the required value that we supplied.0578

And then, because we didn't supply values for the optional parameters 1, 2, or 3, this outputs their default values.0589

Now, let's say we just want to, for example, provide the first and third optional parameter.0598

Well, there is a way to do that in PHP.0603

This messages is just outputting a little HTML comment, saying we are just providing required and third optional argument0606

(actually, for this example, we are just going to provide the required and only the third optional argument).0615

What that means is: in order to do that, because we want to provide the third optional argument only, and not the first or the second,0621

in order to do that, we have to provide values for both of those optional parameters.0629

If, for example, let's say we just set the required parameter, and we just want to set the third optional parameter,0636

and we say param3 value, what is going to happen is: because this is listed as the second parameter,0645

even though we want it to set the parameter optional3, because it is provided second,0659

it is actually going to set the value of the optional1 parameter.0666

So, what we have to do is provide some sort of value for those other optional parameters.0670

In this case, we supply a value null; so, what that is going to do, when we call this function, is set required equal to this string required value.0679

It is going to set the value of parameter optional1 and parameter optional2 both to the values of null.0692

And when we try to output them, it is going to output the empty string, because null converted to a string is the empty string.0699

And then, we have set parameter 3 to this string value here, so when optional3 is output, that string will be output.0705

So, if we go ahead and save this and reload it in the browser, in our new section down here that says0712

we are just providing the required and third optional arguments, we can see that the third value was output.0718

optional1 and 2 were not output, because they had to be provided a value, and we provided them the value null; and parameter 3 was output.0724

Now, the other thing that we can do is: if we only want to provide (for example) the first two optional parameters,0736

along with the required one, we don't have to do anything special and provide a value for this third optional value.0746

Whereas here, because we wanted to provide the third value, we had to provide sample values for the other two optional parameters,0752

if you are providing optional parameters that are to the left of other optional parameters (for example, optional1 and optional20762

are to the left of optional3 in the parameter list), and you only want to provide these first two, you can just leave the last one out.0772

So, when we call this function a third time, we only have to provide values for optional1 and optional2,0780

and we don't have to provide anything extra down here.0814

For example, we don't have to add a comma and then provide null to provide a value for the optional value.0816

So, if we save this and go ahead and load the page, we are going to get a third section of output.0824

As you can see, we have the required value that was output; these strings provided for the optional1 and optional2 parameters are output.0831

And because we didn't provide anything for optional3, the default value was output.0840

And as you can see, up here the default value didn't get output, because we had to provide a value for them,0845

and in that case, we provided the value null.0849

One important thing to note about optional parameters is that optional parameters, when you define a function with them,0855

always need to be listed in the parameter list to the right of any required parameters.0862

For example, we have a required parameter here; and we define that first, and then any optional parameters.0869

And the reason for that is because if you had, for example, defined optional first, and (let's say) put the required parameter last,0877

what is going to happen is: when we call the function, we are going to get an error.0898

For example, down here, where we have just provided the required value, I'm going to go ahead and run this script.0900

It is going to say "you are missing argument 4 for optional," for example.0911

And the reason that is: in this first function, called optional, we supplied one argument; it is going to set optional1 equal to that.0917

Then, for optional2 and optional3, we have default values; but because required0926

is not an optional parameter and doesn't have a default value, it is going to say you are missing that value.0932

Now, the way we can get around that would be to provide values for the optional ones.0938

And when we run it, we are not going to get that problem; but that is not a desirable thing to do.0947

And then, it is also going to make us have to provide blank values for all of the optional arguments, when we don't need to--0956

when, if we just list the required parameter first, then we don't have that problem.0962

And so, that is why required parameters should always be listed first in a function's parameter list.0967

For the homework challenge, I am going to have you create a function called add0982

that is going to have two required parameters, one called op1 and one called op2,0985

and then two optional parameters, called op3 and op4.0990

What the function is going to do is sum the values of the two required parameters, op1 and op2.0994

And then, if op3 and op4 are provided, it is going to also add that to the sum.1001

And it is going to return the total of all of the op parameters that were provided.1006

So, in order to get practice calling functions with optional parameters, I want you to call the function four times:1012

once where you just provide the two required parameters, op1 and op2; one where you provide all 4 parameters1020

(so it is going to add four different values); one where you provide the two required parameters and the first optional parameter1028

(which is the third parameter of the function)...1036

That is going to show you how to make a function call that has optional arguments,1039

where you don't have to supply a value for any other optional arguments that you don't wish to.1047

For example, we don't have to supply an op4 value here, because in our function definition, op3 is going to be defined first.1053

Then, I want you to call the function, providing op1, op2, and op4.1062

What that is going to do is give you practice calling an optional argument function,1067

where you are supplying an optional parameter, a value, that isn't the leftmost optional parameter.1073

So, when we call the function in this case, we are going to provide (for example) two values for the first two.1079

And, because we only want to provide op4, we have to provide some value in here.1089

It could be null, for example, but we have to provide that; and that is going to give you practice with calling functions1096

with optional parameters that have multiple optional parameters.1105

The one thing to note is that you need to define appropriate default values for both of your optional parameters,1109

because what you are going to need to do is...for example, if we provide this value null inside the function,1115

you are going to want to test to see if null was provided; and if so, you are not going to add that to the total.1125

The other thing is: let's say, in our function, that this is part of the function declaration--this parameter list.1130

Let's say, for example, that you set the default value of op3 equal to null.1147

That way, you can test in your code if op3 is equal to null; that means it wasn't provided,1150

or it was provided the value of null, and you don't need to add it.1155

And so, that way, you can additionally add op3 or op4.1158

You can set it to other values; you could set it to a string (the empty string, for example) and test for that; that is another option, as well.1161

That ends today's lesson on optional parameters; thank you for watching, and I look forward to seeing you next time.1170