PHP Reference ยท Language Constructs and Statements
PHP const and define()
Reference for PHP const and define(): declare constants at compile time or run time, naming rules, array constants, namespaces and class constants.
A constant is a named value that cannot change once it is set. PHP offers two ways to create global constants: the const keyword and the define() function. Both create a name you use without a $, and both are visible in every scope.
The difference is when they run. const is a language construct that must appear at the top level of a file or namespace (not inside if, loops or functions), and its value is fixed when the file is compiled. define() is an ordinary function call, so it can run conditionally and take a computed name.
const MAX_USERS = 100; define('APP_ENV', 'production'); echo MAX_USERS, " ", APP_ENV;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
const NAME = expression; const A = 1, B = 2; define(string $constant_name, mixed $value, bool $case_insensitive = false): bool
| Name | Description |
|---|---|
NAME / $constant_name |
The constant's name. Letters, digits and underscores, not starting with a digit. By convention UPPER_SNAKE_CASE. |
expression / $value |
A scalar, null, array or enum case. const accepts only constant expressions (literals, other constants, operators); define() accepts any runtime value, including objects since PHP 8.1. |
$case_insensitive |
Obsolete. Case-insensitive constants are no longer supported; passing true only raises a warning. Leave it out. |
Return value (define): true on success, false if the constant already exists (with a warning).
Errors: reading an undefined constant throws Error ("Undefined constant"), a change from the warning and string fallback of PHP 7.
Usage notes
constfor fixed names,define()for dynamic ones. Useconstby default; it is faster, works with namespaces and is easier to read. Reach fordefine()when the name or the condition is only known at run time.- Namespaces.
constinsidenamespace App;createsApp\NAME, whiledefine('NAME', ...)creates a global unless you include the namespace in the string:define('App\NAME', 1). - Constants never change. Redefining returns
falsewith a warning, and you cannotunset()one. - Arrays. Array constants are allowed and read-only:
LIMITS['max']works, butLIMITS['max'] = 5is a compile error. - Check before reading.
defined('NAME')tests existence;constant('NAME')reads a name held in a string and also works forClass::NAME. - Class constants are declared with
constinside a class and read withClass::NAMEorself::NAME. They can have visibility,final, and (PHP 8.3) a type; see Class constants. - Not case-insensitive.
true,falseandnullare the only special-cased words; all your own constants are case-sensitive. - Magic and predefined constants such as
__LINE__andPHP_VERSIONare listed in Magic constants and Predefined constants. - Configuration. For settings that differ between environments, prefer reading environment variables over constants hardcoded in code.