Default parameter

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 "...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-16-2004
Mike
 
Posts: n/a
Default 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...
}
Reply With Quote
  #2 (permalink)  
Old 11-16-2004
Michael Fesser
 
Posts: n/a
Default Re: Default parameter

.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
Reply With Quote
  #3 (permalink)  
Old 11-16-2004
Pedro Graca
 
Posts: n/a
Default Re: Default parameter

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.
Reply With Quote
  #4 (permalink)  
Old 11-16-2004
Mike
 
Posts: n/a
Default Re: Default parameter

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

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:21 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0