Thursday 4 July 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...