471,896 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,896 software developers and data experts.

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 102237
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
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.