By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

Function definition with optional parameters?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.