PHP constants are similar to variables. Constants are given a name, and a value is stored in them. However, they can’t be changed by the script. After you set the value for a constant, it stays the same. For example, if you used a constant for age and set it to 21, the value is always and forever 21.
Constants are used when a value is needed in several places in the script and doesn’t change during the script. The value is set in a constant at the start of the script. By using a constant throughout the script, instead of a variable, you make sure that the value won’t get changed accidentally.
Creating a constant is done by calling the define() function, and passing a string of your constant’s name and its value.
For example, to set a constant with the company name, use the following statement:
define('COMPANY','My New Company');
You can use any name for a constant that you can use for a variable, as long as you follow these conventions:
No identifier: Constant names are not preceded by a dollar sign ($).
Case: By convention, constants are given names that are all uppercase, so you can easily spot constants, but PHP itself doesn’t care what you name a constant.
Characters: You can store either a string or a number in it.
The value for define may be any scalar data type: integer, float, string, or bool. The define function allows an expression to be used in the assignment, such as a variable or the result of a mathematical expression.
define('ONE', 1); // 1
define('TWO', ONE+1); // 2
Constants are case sensitive by default. However, the define function takes a third optional argument that may be set to true to create a case-insensitive constant.
The const modifier is used to create class constants. Unlike regular properties, class constants do not have an access level specified because they are always publicly visible. They also do not use the dollar sign parser token ($). The naming convention for constants is all uppercase, with underscores separating each word.
const PI = 3.14;
Constants must be assigned a value when they are created. Like static properties, a constant may only be initialized with a constant value, and not with an expression. Class constants are referenced in the same way as static properties, except that they do not use the dollar sign.
echo MyCircle::PI; // "3.14"
Check for Constant
To check whether a constant already exists, the defined function can be used. This function works for constants created with const or define.
PHP 7 made it possible to create constant arrays using the define function. Support for constant arrays created with const has existed since PHP 5.6.
const CA = [1, 2, 3]; // PHP 5.6 or later
define('DA', [1, 2, 3]); // PHP 7 or later
PHP provides eight predefined constants. These are called magic constants because their values change, depending on where they are used.
- __LINE__ : Current line number of the file.
- __FILE__ : Full path and filename of the file.
- __DIR__ : Directory of the file.
- __FUNCTION__ : Function name.
- __CLASS__ : Class name including namespace.
- __TRAIT__ : Trait name including namespace.
- __METHOD__ : Class method name.
- __NAMESPACE__ : Current namespace.