How do you declare constants in PHP and what's the difference between define() and const?
Beginner
Answer
Constants can be declared using define() function or const keyword:
define(): Can be used anywhere, supports dynamic names and conditional declaration
const: Must be declared at top-level scope, faster at compile time, supports class constants
define('MAX_SIZE', 100);
const MIN_SIZE = 10;
// define() can be conditional
if ($condition) {
define('DYNAMIC', 'value');
}
// const cannot be used conditionally