DEFINE vs Variable in PHP

There is also a difference in scope.

In the example given by the orignal poster, $SOMETHING will not be accessible within a function whereas define('SOMETHING', true) will be.


DEFINE makes a constant, and constants are global and can be used anywhere. They also cannot be redefined, which variables can be.

I normally use DEFINE for Configs because no one can mess with it after the fact, and I can check it anywhere without global-ling, making for easier checks.


Once defined, a 'constant' cannot be changed at runtime, whereas an ordinary variable assignment can.

Constants are better for things like configuration directives which should not be changed during execution. Furthermore, code is easier to read (and maintain & handover) if values which are meant to be constant are explicitly made so.

Tags:

Php