Sunday 15 September 2013

PHP Optional Function Arguments

PHP Optional Function Arguments

I'm a little stuck trying to create a function that takes a single,
optional argument. Instead of this being a string I'd like it to be the
result of a function (or even better, a DateTime object). Essentially - I
want the user to either pass in a DateTime object, or for the function to
resort to todays date if no arguments are supplied. Is this possible with
PHP? By trying to create the new object in the function header as such
function myDateFunction($date = new DateTime()){
//My function goes here.
}
causes PHP to fall over.
Many thanks.

No comments:

Post a Comment