Log in

View Full Version : Default function values



JasonDFR
02-07-2009, 03:06 PM
What is the best way to assign default arguments to function variables when the default is taken from the value of another function.

Example:


function function($startDate = time()) {

echo $startDate;

}

function();

I would like to do the above, but it does not work.

Do you have to test for $startDate inside the function and then set it to time() if it is null? Or is there a better way?

Thanks.

techietim
02-07-2009, 03:09 PM
This is usually the best way to do it:


function function($startDate = null) {
$startDate = is_null($startDate) ? time():$startDate;
}

JasonDFR
02-07-2009, 04:28 PM
Cool. Thanks.

J