0

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?

Nikk
  • 7,384
  • 8
  • 44
  • 90
  • 1
    Possible duplicate of [PHP Error : Fatal error: Constant expression contains invalid operations](https://stackoverflow.com/questions/40171546/php-error-fatal-error-constant-expression-contains-invalid-operations) – AymDev Jun 23 '18 at 11:09
  • 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 Jun 23 '18 at 11:11
  • first read this "http://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 yet – Ernesto Alfonso Jun 23 '18 at 11:17

1 Answers1

1

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.

SynapseIndia
  • 450
  • 2
  • 10