inactive Posted April 8, 2008 Share Posted April 8, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/100071-solved-default-values-for-function-args/ Share on other sites More sharing options...
Northern Flame Posted April 8, 2008 Share Posted April 8, 2008 do you get an error when you tried that? Quote Link to comment https://forums.phpfreaks.com/topic/100071-solved-default-values-for-function-args/#findComment-511691 Share on other sites More sharing options...
inactive Posted April 8, 2008 Author Share Posted April 8, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/100071-solved-default-values-for-function-args/#findComment-511693 Share on other sites More sharing options...
inactive Posted April 8, 2008 Author Share Posted April 8, 2008 any ideas guys? bit early to bump, but i'm quite desperate here... Quote Link to comment https://forums.phpfreaks.com/topic/100071-solved-default-values-for-function-args/#findComment-511697 Share on other sites More sharing options...
PFMaBiSmAd Posted April 8, 2008 Share Posted April 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/100071-solved-default-values-for-function-args/#findComment-511702 Share on other sites More sharing options...
inactive Posted April 8, 2008 Author Share Posted April 8, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/100071-solved-default-values-for-function-args/#findComment-511705 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.