Thursday, July 4, 2019

php - Parse error: syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM)




I have the following code



define("SCRIPT_URL", "");
function ifScriptFolder() {

if(isset(SCRIPT_URL) && !empty(SCRIPT_URL)) {
echo "/".SCRIPT_URL."/";
} else {
echo "/";
}
}


but it's giving me the following error:




Parse error: syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in *(path)* on line 3


Can anyone see what's going wrong here or how to fix it?


Answer



If you trying to determine if constant exists, then try to use defined() or constant(), not isset().






From php.net:




Function defined():




Returns TRUE if the named constant given by name has been defined, FALSE otherwise.




Function constant():





Returns the value of the constant, or NULL if the constant is not defined.







Fixed function:



function ifScriptFolder() {
echo defined('SCRIPT_URL') ? "/" . constant('SCRIPT_URL') . "/" : "/"
}



UPD:



The defined() function is a better solution for this, because it will not emit E_WARNING if constant was not defined.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...