Jump to content

[SOLVED] default values for function args


inactive

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.