469,326 Members | 1,351 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

Function definition with optional parameters?

Some of the functions that are part of PHP have optional parameters. Is
it possible to define your own function so that it has optional
parameters? If so, how is that done?

Thanks,

Phester

vilefesteringscum
at
yahoo

Jul 17 '05 #1
4 102084
On Sun, 28 Sep 2003 20:20:33 GMT, Phester <ph*****@nonexistenthost.com> wrote:
Some of the functions that are part of PHP have optional parameters. Is
it possible to define your own function so that it has optional
parameters? If so, how is that done?


Certainly; you can give the optional parameters default values.

http://www.php.net/manual/en/functions.arguments.php

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2
Phester wrote:
Is
it possible to define your own function so that it has optional
parameters?
Yes.

If so, how is that done?


Define a function, giving the last parameters a default value

<?php
function xyz($a, $b, $c=0, $d='localhost') {
// do nothing :-)
}

### and then call it
xyz(0, 0, 3, 'yahoo.com');
xyz(0, 0, 4); // same as xyz(0, 0, 4, 'localhost');
xyz(0, 0); // same as xyz(0, 0, 0, 'localhost');
?>

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #3
Andy Hassall wrote:
On Sun, 28 Sep 2003 20:20:33 GMT, Phester <ph*****@nonexistenthost.com> wrote:
Some of the functions that are part of PHP have optional parameters. Is
it possible to define your own function so that it has optional
parameters? If so, how is that done?


Certainly; you can give the optional parameters default values.

http://www.php.net/manual/en/functions.arguments.php

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

Thanks!

Phester

Jul 17 '05 #4
Pedro wrote:
Phester wrote:
Is
it possible to define your own function so that it has optional
parameters?

....
Define a function, giving the last parameters a default value

<?php
function xyz($a, $b, $c=0, $d='localhost') {
// do nothing :-)
}

### and then call it
xyz(0, 0, 3, 'yahoo.com');
xyz(0, 0, 4); // same as xyz(0, 0, 4, 'localhost');
xyz(0, 0); // same as xyz(0, 0, 0, 'localhost');
?>

Thanks, that's exactly what's required.

Phester

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Programmer | last post: by
2 posts views Thread by Frank Rizzo | last post: by
reply views Thread by lakshmi | last post: by
1 post views Thread by Glenn | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.