Default parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    #1

    Default parameter

    If possible, how do I use a function to create a default parameter
    value? The following gives me an "unexpected '(', expecting ')'"
    error, which makes sense.

    function DisplayInfo($ID , $startdate=date ("Ydm"), $numdays=7) {
    ...function here...
    }
  • Michael Fesser

    #2
    Re: Default parameter

    .oO(Mike)
    [color=blue]
    >If possible, how do I use a function to create a default parameter
    >value? The following gives me an "unexpected '(', expecting ')'"
    >error, which makes sense.
    >
    >function DisplayInfo($ID , $startdate=date ("Ydm"), $numdays=7) {
    > ...function here...
    >}[/color]

    Default arguments have to be constant values, no variables or function
    calls. You could use something like this:

    function DisplayInfo($ID , $startdate = NULL, $numdays = 7) {
    if (is_null($start date)) {
    $startdate = date('Ydm');
    }
    ...
    }

    Micha

    Comment

    • Pedro Graca

      #3
      Re: Default parameter

      Mike wrote:[color=blue]
      > If possible, how do I use a function to create a default parameter
      > value?
      >
      > The following gives me an "unexpected '(', expecting ')'"
      > error, which makes sense.
      >
      > function DisplayInfo($ID , $startdate=date ("Ydm"), $numdays=7) {
      > ...function here...
      > }[/color]


      The default value has to be a constant. It cannot be a variable or a
      class member or (as you noticed) a function call.


      function DisplayInfo($ID , $startdate=NULL , $numdays=7) {
      if (is_null($start date)) $startdate = date('Ydm');
      ...function here...
      }


      --
      Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
      == ** ## !! !! ## ** ==
      TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
      bypass the spam filter. I will answer all pertinent mails from a valid address.

      Comment

      • Mike

        #4
        Re: Default parameter

        Thanks for clearing that up. I ended up using '$startdate="bl urgh"'

        if ($startdate="bl urg"){
        $startdate=date ("Ymd");
        }

        Michael Fesser <netizen@gmx.ne t> wrote in message news:<9diip01h7 eqforq6e8h0ucmr 6sk7tl29jq@4ax. com>...[color=blue]
        > .oO(Mike)
        >[color=green]
        > >If possible, how do I use a function to create a default parameter
        > >value? The following gives me an "unexpected '(', expecting ')'"
        > >error, which makes sense.
        > >
        > >function DisplayInfo($ID , $startdate=date ("Ydm"), $numdays=7) {
        > > ...function here...
        > >}[/color]
        >
        > Default arguments have to be constant values, no variables or function
        > calls. You could use something like this:
        >
        > function DisplayInfo($ID , $startdate = NULL, $numdays = 7) {
        > if (is_null($start date)) {
        > $startdate = date('Ydm');
        > }
        > ...
        > }
        >
        > Micha[/color]

        Comment

        Working...