- define ( string $name , mixed $value [, bool $case_insensitive = false ] )
- const CONSTANT_NAME = VALUE;
Constants are used to store the values that are not supposed to be changed later. They also are often used to store the configuration parameters especially those which define the environment (dev/production).
Constants have types like variables but not all types can be used to initialize a constant. Objects and resources cannot be used as values for constants at all. Arrays can be used as constants starting from PHP 5.6
Some constant names are reserved by PHP. These include
null as well as many module-specific constants.
Constants are usually named using uppercase letters.
Checking if constant is defined
To check if constant is defined use the
defined function. Note that this function doesn't care about constant's value, it only cares if the constant exists or not. Even if the value of the constant is
false the function will still return
Note that constant becomes "visible" in your code only after the line where you have defined it:
Getting all defined constants
To get all defined constants including those created by PHP use the
To get only those constants that were defined by your app call the function at the beginning and at the end of your script (normally after the bootstrap process):
It's sometimes useful for debugging
Constants can be defined inside classes using a
This is useful to store types of items.
Arrays can be used as plain constants and class constants from version PHP 5.6 onwards:
Class constant example
Plain constant example
Also from version PHP 7.0 this functionality was ported to the
define function for plain constants.
Constants are created using the
const statement or the
define function. The convention is to use UPPERCASE letters for constant names.
Define constant using explicit values
Define constant using another constant
if you have one constant you can define another one based on it:
Some constant names are reserved by PHP and cannot be redefined. All these examples will fail:
And a Notice will be issued:
If you have several files where you may define the same variable (for example, your main config then your local config) then following syntax may help avoiding conflicts:
define is a runtime expression while
const a compile time one.
define allows for dynamic values (i.e. function calls, variables etc.) and even dynamic names and conditional definition. It however is always defining relative to the root namespace.
const is static (as in allows only operations with other constants, scalars or arrays, and only a restricted set of them, the so called constant scalar expressions, i.e. arithmetic, logical and comparison operators as well as array dereferencing), but are automatically namespace prefixed with the currently active namespace.
const only supports other constants and scalars as values, and no operations.
To use the constant simply use its name:
or if you don't know the name of the constant in advance, use the