Make Parameter Optional

Ask about a PHP problem here.
Post Reply
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Make Parameter Optional

Post by unemployment »

How do I make a function parameter optional?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make Parameter Optional

Post by jacek »

Give it a default value in the definition
function I_am_a_function($first, $second = true){
    // things
}
if no value is given for the second parameter, the code inside the function will see it as true.
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Make Parameter Optional

Post by unemployment »

jacek wrote:Give it a default value in the definition
function I_am_a_function($first, $second = true){
    // things
}
if no value is given for the second parameter, the code inside the function will see it as true.
I assume I can set = null
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Make Parameter Optional

Post by unemployment »

yay... you can
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make Parameter Optional

Post by jacek »

unemployment wrote:yay... you can
:lol:

I think you can set = pretty much anything.
Image
Post Reply