Declare a constant with the const keyword or the define() function. The main difference is timing. const is defined at compile time, so it must sit at the top level of a file or inside a class. define() runs at runtime like any function call, so it works anywhere, including inside an if.
define('MAX_SIZE', 100);
const MIN_SIZE = 10;
// define() can be conditional
if ($condition) {
define('DYNAMIC', 'value');
}
// const cannot be used conditionally
Because of that, const cannot appear inside functions, loops or if and try blocks. define() can also build the constant's name at runtime and accepts any expression as the value. Only const can declare class constants.
Prefer const for fixed values, and keep define() for constants that are conditional or have computed names.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓