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
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Sun Dec 18, 2005 9:41 am
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
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Sun Dec 18, 2005 9:58 am
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 » Sun Dec 18, 2005 10:05 am
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 » Sun Dec 18, 2005 10:22 am
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Sun Dec 18, 2005 10:27 am
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 » Sun Dec 18, 2005 10:30 am
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 » Sun Dec 18, 2005 10:33 am
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
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Sun Dec 18, 2005 10:45 am
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.