Page 1 of 1

Optional parameters for custom functions.

Posted: Sun Dec 22, 2002 12:59 pm
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

Posted: Sun Dec 22, 2002 1:32 pm
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.

Posted: Mon Dec 23, 2002 1:22 am
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 ;)

Posted: Mon Dec 23, 2002 12:53 pm
by Owe Blomqvist
Thank you guys.
That worked like a charm.

best regards,
Owe Blomqvist