const vs. define()

The fundamental difference between those two ways is that const defines constants at COMPILE time, whereas define defines them at RUN time. This causes most of const’s disadvantages. Some disadvantages of const are:

const cannot be used to conditionally define constants. To define a global constant, it has to be used in the outermost scope:

if (...) {
      const FOO = 'BAR';     // invalid
}

if (...) {
      define('FOO', 'BAR');   // valid
}

const accepts a static scalar (number, string or other constant like true, false, null, __FILE__), whereas define() takes any expression.

consts are always case sensitive, whereas define() allows you to define case insensitive constants by passing true as the third argument:

define(‘FOO’, ‘BAR’, true);
echo FOO; // BAR
echo foo; // BAR

Вашият коментар

Вашият имейл адрес няма да бъде публикуван. Задължителните полета са отбелязани с *