Constants¶
Constants are named values, that never change.
Constants may be created with the const keyword or the define() function. They may be tested for existence with the defined() function.
There are global constants, which are accessible with their name and their namespaces. There are also constants in classes, interfaces, enums and traits. Those are called class constant.
Constants are namespace dependent. They can be imported using the use const command.
Constants are usually written in uppercase.
Global constants used to be case insensitive, when created with define(). This feature was removed in PHP 8. Nowadays, both const and define create case insensitive values.
<?php
const A = 1;
define('B', 2);
// displays 1
echo A;
?>
See also Understanding PHP Constants: A Simple Guide with Examples.
Related : Static Constant, Centralization, Constant Scalar Expression, Dynamic Constant, New In Initializers, Predefined Constants, Class Constant Syntax, Constant Case, define(), Literal, Anonymous Constant, DIRECTORY_SEPARATOR, Dynamic Class Constant, Hard Coded, Magic Numbers, Order Of Execution