Optional parameters for custom functions.

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
Owe Blomqvist
Forum Commoner
Posts: 33
Joined: Wed Oct 16, 2002 2:27 pm

Optional parameters for custom functions.

Post by Owe Blomqvist »

Hi.
I wonder how i should do to have parameters optional and not required
for custom functions, like some of the built in function.
For example.
for my site i have this function called:
send_message( $message, $notice ), where $notice should be a optional parameter.

It simply draws a message on screen.
Now, if it is a warning you should set parameter 2 to:

1 = notice,
2 = varning,
3 = critical

If i leave $notice blank it just gives me the "wrong parameter count for function..." message.

Thank you for your time,
Best Regards,
Owe Blomqvist
User avatar
PaTTeR
Forum Commoner
Posts: 56
Joined: Wed Jul 10, 2002 7:39 am
Location: Bulgaria
Contact:

Post by PaTTeR »

Code: Select all

function test($required,$not_required='') {
  ........
}
Now you can use both

Code: Select all

test('hallo');


test ('hallo','world');
But keep in mind, if you use moore optional paramters.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if your function is documented well you migh consider using http://www.php.net/manual/en/function.func-num-args.php
Without a proper doc this gets quite confusing ;)
Owe Blomqvist
Forum Commoner
Posts: 33
Joined: Wed Oct 16, 2002 2:27 pm

Post by Owe Blomqvist »

Thank you guys.
That worked like a charm.

best regards,
Owe Blomqvist
Post Reply