building functions with optional parameters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

building functions with optional parameters

Post by malcolmboston »

im building a function atm that i would like to be able to pass an optional 2nd parameter, how could this be written without throwing a PHP error

Code: Select all

function ExampleFunction ($Mode, $Filter)
{
     // only run certain code if $filter has been passed
}
obviously i could preset $filter = "NONE", and do it based that but, you can do it with standard PHP functions so i am interested in doing it this way
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: building functions with optional parameters

Post by onion2k »

Code: Select all

function ExampleFunction ($Mode, $Filter="NONE")
{
     // only run certain code if $filter has been passed
}
If $filter isn't passed then it'll default to NONE.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

if filter isnt passed then it will throw up a PHP error, internal PHP functions allow you to do this....am i explaining what i mean badly?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

http://www.php.net/manual/en/function.func-get-arg.php
Is this what you mean and want to implement?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

Code: Select all

function ExampleFunction ($Mode, $Filter) 
{ 
     // only run certain code if $filter has been passed 
}
example for call usage

i would have to do this

Code: Select all

ExampleFunction ($Mode = "MODE1", $Filter = "NONE")

Code: Select all

// i want to do this
ExampleFunction ($Mode = "MODE1);
basically assuming $filter is not passed there is not one set, however i would get an invalid arguments passed error
acidHL
Forum Commoner
Posts: 41
Joined: Wed Dec 07, 2005 7:38 am

Post by acidHL »

Onion2k's post does what you want to do.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

i know that, what im saying is that i dont want to do it that way if possible, various internal PHP functions take optional arguents that are only used if you pass the parameter too them, why cant i do it and if i can, whats the syntax
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

malcolmboston wrote:i know that, what im saying is that i dont want to do it that way if possible, various internal PHP functions take optional arguents that are only used if you pass the parameter too them, why cant i do it and if i can, whats the syntax
I see what you mean.

Code: Select all

function blah($a,$b=NULL) {
  if (isset($b)) { echo "something"; } else { echo "nothing"; }
}
You'll still need to detect if the optional parameters are set though .. it's really no less work. PHP's internal functions are doing that too.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

thank you
Post Reply