Results 1 to 3 of 3

Thread: Default function values

  1. #1
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default Default function values

    What is the best way to assign default arguments to function variables when the default is taken from the value of another function.

    Example:

    PHP Code:
    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.

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    This is usually the best way to do it:
    PHP Code:
    function function($startDate null) {
      
    $startDate is_null($startDate) ? time():$startDate;


  3. The Following User Says Thank You to techietim For This Useful Post:

    JasonDFR (02-07-2009)

  4. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Cool. Thanks.

    J

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •