Page 1 of 1

Default ARGUE-ments

Posted: Mon May 30, 2005 12:02 am
by harrisonad
Hi again,
I am confused now and then while editing my recent project. This is about 'default arguments'.
I have here a function similar to what i have posted last time, only different now.

Code: Select all

function foo($name='no name',$address='no address',$age=0){
So I can call this function is so many ways.

Code: Select all

// full arguments
foo('Harrison','Philippines',23);
// no age 
foo('Harrison','Philippines');
// name only
foo('Harrison');
// etc...
But what if I want to give only the age? Of course, I can do this:

Code: Select all

foo('','',23);
// or using constants
foo(NO_NAME,NO_ADDRESS,23);
But the default arguments will not be followed, giving both the name and address argument a NULL value. Right?

And if a don't define argument and let the 'func_num_args()' do the job, How can I know if I what I passed is the name or the address argument assuming that they have the same data type?

I want to give a value for the third argument and default values for the first two arguments.
How can I make this happen? Any Ideas?...

Posted: Mon May 30, 2005 12:21 am
by programmermatt
I do it in this method:

Code: Select all

function foo( $arg1=null, $arg2=null, $arg3=null ) {

if( $arg1 === null ) {
 //default value substitution
 $arg1 = "default";
}
if( $arg2 === null .....
This allows you to do something like this:

Code: Select all

foo(null,null,12);
Just how I do it.

Posted: Mon May 30, 2005 12:22 am
by Skara
well, you could pass it as so:
foo(0,0,'whatever');
and then just use a couple of ifs to set the first two if they're blank. *shrugs*