This is a discussion on Default parameter within the PHP Language forums, part of the PHP Programming Forums category; If possible, how do I use a function to create a default parameter value? The following gives me an "...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
.oO(Mike)
>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... >} 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($startdate)) { $startdate = date('Ydm'); } ... } Micha |
|
|||
|
Mike wrote:
> 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... > } 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($startdate)) $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. |
|
|||
|
Thanks for clearing that up. I ended up using '$startdate="blurgh"'
if ($startdate="blurg"){ $startdate=date("Ymd"); } Michael Fesser <netizen@gmx.net> wrote in message news:<9diip01h7eqforq6e8h0ucmr6sk7tl29jq@4ax.com>. .. > .oO(Mike) > > >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... > >} > > 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($startdate)) { > $startdate = date('Ydm'); > } > ... > } > > Micha |
![]() |
| Thread Tools | |
| Display Modes | |
|
|