Search code examples
phpvariablesglobal-variablesconstants

What is the best way to define a global constant in PHP which is available in all files?


What is the best way to define a constant that is global, i.e., available in all PHP files?

UPDATE: What are the differences between constants and class constants? Can they be used interchangeably?


Solution

  • Define your constant in your top .php file, that will be included in all the other scripts. It may be your front controller, your config file, or a file created for this single purpose.

    !defined('MY_CONST') && define('MY_CONST', 'hello');