Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed PHP Functions!
You have completed PHP Functions!
Preview
Sometimes if an argument is not passed through, and you want it to have a starting value, no matter what, this is where a default argument will come to your assistance.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Having a good understanding of what a
function argument means,
0:01
we can now talk about another important
component known as default arguments.
0:03
Sometimes, if an argument is not passed
through, but
0:08
you want the function to have a starting
value no matter what,
0:11
this is where a default argument will come
to your assistance.
0:15
Now you can see it's just hello, dollar
sign, name.
0:18
Then, we'll simply add name equals and
0:22
then whatever our definition for the
default argument should be.
0:26
In this case we'll just do friend that way
I can pass through an argument like this.
0:30
So I'll say hello and then Mike.
0:34
That way it'll say Hi, Mike, how's it
going?
0:37
But if I choose to just pass through
hello,
0:40
remember we didn't define anything outside
of the function scope like before, but
0:43
now it's gonna say Hi, friend, how's it
going?
0:47
Now what if we wanted to do multiple
default arguments,
0:50
just like in our greet function?
0:54
We'll simply add the same way we did it
before, which is equals friend and
0:55
then equals morning on time_of_day and
name, respectively.
1:00
This way I can still call, greet Mike
afternoon and
1:04
get, Hi, Mike, good afternoon.
1:08
But if I pass with no arguments
whatsoever, it'll just say, Hi, friend,
1:11
good morning.
1:16
Now let's say we wanted to make an
argument predefined as optional.
1:17
Well the way we do that is in the same way
we've done default arguments.
1:22
Let's say for instance, I don't really
need you to provide me a time of day.
1:26
Well, let's do that by simply eliminating
it set to morning and
1:30
make it equal to Null.
1:35
This way, we have to check inside the
function if time of day is set.
1:37
So we'll check if time_of_day, and
1:42
then we'll surround our previous statement
with an if and an else.
1:44
Now, if time_of_day is equal something
other than null,
1:49
it'll say hi name and then good
time_of_day.
1:52
But we need to provide an else as well.
1:56
So what we'll do is just say echo hello
comma name.
1:59
And close that statement with a semicolon.
2:04
This way I can still say greet Mike
afternoon and get, Hi, Mike,
2:07
good afternoon.
2:12
But I'm also able to just pass through
Mike to our
2:13
greet function without the time of day and
we'll end up getting this back.
2:16
Hi, Mike.
2:20
So now let's talk a look here in
Workspaces on how to
2:22
work with default arguments.
2:25
So far we have had some arguments here,
we've passed through arguments,
2:27
if we don't pass an argument we'll end up
getting an error.
2:32
So what we'll need to do is create default
arguments so
2:35
that way we don't get the errors when we
forget to pass through.
2:38
We'll also look at how to make arguments
optional.
2:42
So, let's start with creating a function
that will show a little
2:45
bit of info about someone and maybe who
they are or a title.
2:48
So let's start with creating that
function.
2:53
So function and then get info.
2:56
'Kay.
And we're gonna have two argument here,
3:01
one is gonna be the name and next one is
going to be the title.
3:03
'Kay?
3:07
Then open and close our curly braces.
3:09
So now we have our function.
3:12
Now inside of this function what we want
to do is just echo a string.
3:14
So echo, and then we'll go ahead and
3:17
do double quotes even though we're going
to output some variable information.
3:20
So let's start with name and then has
arrived,
3:26
they are with us as a and then title.
3:32
So this way in a string format if I was to
say get info.
3:38
And then pass through two strings.
3:45
So let's say Mike and Mike is a frog.
3:46
Okay.
So we have multiple arguments here and
3:51
we have one is the name and next will be
in the title.
3:54
So notice they are in the same order as
they were defined on line three.
3:58
So I'm going to go ahead and save this and
then take out, take a look at the preview.
4:01
So here we see it says Mike has arrived.
4:06
They are with us as a frog.
4:09
Okay.
That seems perfectly reasonable.
4:11
Let's go back over and say that we forget
to type in frog.
4:13
We just save this and hit refresh.
4:19
It'll say Mike has arrived.
4:21
They are with us as a, nothing.
4:22
There's a blank.
4:25
Well we don't want that so
4:25
we're gonna need to define some kind of
default argument here.
4:27
So let's go back over to our code and then
say, get info, title, and name.
4:31
Well let's say we want to define a title
to start with so
4:37
we say title equals, and this is where our
default is.
4:41
And we'll say, friend.
4:45
All right, now save it.
4:46
And if we don't use title as an argument
it should come through as friend.
4:50
So we'll go ahead and hit our Preview.
4:55
Refresh.
4:58
And there it says, Mike has arrived, they
with us as a friend.
4:59
Which is great.
5:02
Okay.
5:03
Now what if we wanted to take, and make
the title completely optional?
5:04
Well, that's pretty easy to do.
5:09
All we need to do is remove equals friend,
and just simply say equals null.
5:12
Okay, this way it's not gonna report back
any kind of errors or notices for us.
5:17
But we need to do something a little
different.
5:23
Cuz otherwise, it's just gonna come up
with that blank again.
5:24
So let's create an if statement to test
whether or not title exists.
5:27
So we'll say if, and then we'll say title.
5:31
We don't need to see if it's true or
anything like that.
5:36
Just simply saying if title, make sure
that it exists.
5:38
It's anything other than null.
5:41
All right.
So if title.
5:43
We're gonna open and close our curly
braces, oh and then else.
5:45
We're going to do something different.
5:48
All right.
5:50
Well I'm actually going to actually use
this line.
5:51
So, I'm gonna cut it, and then head back
up here and paste it in.
5:53
Make sure my tabs are good.
5:57
Okay.
5:58
And then here if we don't have a title, we
want to echo something different.
5:59
So, echo and then name has arrived, and
then welcome.
6:02
Perfect, all right.
6:10
So now we close that with a semicolon to
end our statement.
6:12
So if we go down and
6:17
run what we have currently by going to our
preview button and refreshing it.
6:18
Now it says, Mike has arrived, welcome.
6:22
Because on line 13 here we did not pass
through an argument.
6:24
But if we do pass through an argument here
such as let's say, frog.
6:29
Save it.
6:36
And then refresh our preview.
6:36
Now it will say, Mike has arrived, they
are with us as a frog.
6:39
So, in order to get a optional argument we
can simply
6:42
pass through the default argument as null,
or we can pass through a string or
6:45
any kind of value we want to use as our
default argument
6:50
in the argument definition of the function
such as in line three.
6:54
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up