Monday, 21 August 2017

parsing - php unexpected ')' parse error










I am trying to set default value for a function. I want the function $expires default value to be time() + 604800.



I am trying



public function set($name,$value,$expires = time()+604800) {
echo $expires;
return setcookie($name, $value, $expires);
}


But I get an error.



Parse error: syntax error, unexpected '(', expecting ')' in /var/www/running/8ima/lib/cookies.lib.php on line 38



How should I write it?


Answer



$expires = time()+604800


in the function definition.



Default value can't be the result of a function, only a simple value
QUoting from the manual:




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




Use:



public function set($name,$value,$expires = NULL) { 
if (is_null($expires))
$expires = time()+604800;
echo $expires;
return setcookie($name, $value, $expires);
}

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...