Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 217
cldy1020 Avatar asked May 21 '12 19:05

cldy1020


1 Answers

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'); 
like image 105
Samy Dindane Avatar answered Sep 24 '22 08:09

Samy Dindane