Jump to content

Recommended Posts

not sure if i have my terminology right here, but I know you can set a value for an argument in a function like so

<?php
function dosomething($arg1='foo') {
...
}
?>

so that when the function is called, if $arg1 was not passed, it will take the value of 'foo' instead of throwing up an error

 

why can't you do something like this?:

<?php
function dosomething($arg1= date("Y")) {
...
}
?>

is there a way to do that?

Link to comment
https://forums.phpfreaks.com/topic/100071-solved-default-values-for-function-args/
Share on other sites

Yes i did...

 

So my actual function:

<?php
function foobar($from_day = date("j"), $from_month = date("n")-2, $from_year = date("Y"), $to_day = date("j")+1, $to_month = date("n"), $to_year = date("Y"))  {
...
}
...
?>

 

And the error I get is:

 

Parse error:  syntax error, unexpected '(', expecting ')' in C:\server\Apache\htdocs\test.php on line 43

 

Where line 43 is the one above. Have I made some other kind of syntax error i missed?

I trust you saw this in the php manual -

 

The default value must be a constant expression, not (for example) a variable, a class member or a function call.

 

hmm yes I really should read that more...ok i'll have to work something else out then...

 

thanks guys...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.