0
votes

I am trying to include $_SERVER['DOCUMENT_ROOT'] into a constant which I will use to define the path.

However I get an error thrown: Fatal error: Constant expression contains invalid operations

const config_path = $_SERVER['DOCUMENT_ROOT'].'folder/';

Am I doing something wrong?

1
Use define('config_path', $_SERVER['DOCUMENT_ROOT'].'folder/'); because you can't concatenate strings using const. Also, declaring a constant in uppercase is a common practice.AymDev
first read this "php.net/manual/es/reserved.variables.server.php" then the constants are similar as static you can not make a constant equal as a var because when their are created the var not exist yetErnesto Alfonso

1 Answers

1
votes

You should use define('config_path', $_SERVER['DOCUMENT_ROOT'].'folder/'); As const would be evaluated at compile time where as define would be evaluated at run time. So using $_SERVER variable with const would casue the error.