PHP Constants


Constants in PHP are identifiers whose value cannot be changed once defined.

Once a constant value is defined, it cannot be changed anywhere else in the script.

Constants can be defined using thedefine()function orconstkeyword.

Features of Constants


PHP Constants

  1. Immutability: Once a constant is defined, its value cannot be changed.
  2. Global scope: After a constant is defined, it can be used anywhere in the entire script without using theglobalkeyword.
  3. Data types: The value of a constant can be a scalar data type (such as boolean, integer, float, string) or an array (PHP 7 and above).
  4. Case sensitivity: Constant names are case-sensitive by default. If you need to define a case-insensitive constant, you candefine()set the third parameter of the function totrue。

A constant is an identifier for a simple value that cannot be changed in the script.

A constant consists of English letters, underscores, and digits, but digits cannot appear as the first character. (Constant names do not need the $ modifier.)

Note:Constants can be used throughout the entire script.


Setting PHP Constants

To set a constant, use the define() function, whose syntax is as follows:

bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )

The function has three parameters:

  • name:Required parameter: the constant name, i.e., the identifier.

  • value:Required parameter: the value of the constant.

  • case_insensitive : Optional parameter. If set to TRUE, the constant will be case-insensitive; the default is case-sensitive.

    Note:Since PHP 7.3.0, defining case-insensitive constants has been deprecated. As of PHP 8.0.0, only false is an acceptable value; passing true will generate a warning.

In the following example, we create acase-sensitive constant(not recommended after PHP 7.3), with the constant value "Welcome to example.com":

Example

<?php //Case-sensitive constant name define("GREETING", "Welcome to example.com"); echo GREETING; //Output "Welcome to example.com" echo '<br>'; echo greeting; //Output "greeting", but with a warning indicating that the constant is undefined. ?>

In the following example, we create acase-insensitive constant, with the constant value "Welcome to example.com":

Example

<?php //Case-insensitive constant name define("GREETING", "Welcome to example.com", true); echo greeting; //Output "Welcome to example.com" ?>

Constants are Global

After a constant is defined, it is a global variable by default and can be used anywhere in the entire running script.

The following example demonstrates using a constant inside a function; even if the constant is defined outside the function, it can still be used normally.

Example

<?php define("GREETING", "Welcome to example.com"); function myTest() { echo GREETING; } myTest(); //Output "Welcome to example.com" ?>

Using the const Keyword

const CONSTANT_NAME = "value";

The following is an example of defining a constant using the const keyword:

Example

const SITE_URL = "https://www.example.com";
echo SITE_URL; // Output "https://www.example.com"

Predefined Constants

PHP provides some predefined constants that can be used directly in scripts. These constants are commonly used to obtain PHP configuration information, version information, etc. Common predefined constants include:

  • PHP_VERSION: The version of the current PHP parser.
  • PHP_OS: The operating system of the server.
  • PHP_INT_MAX: The maximum integer value.
  • E_ERROR、E_WARNING、E_PARSEetc.: Error reporting levels.

Example

echo PHP_VERSION; // Output PHP version, e.g. "7.4.1"
echo PHP_OS;      // Output operating system, e.g. "Linux"
echo PHP_INT_MAX; // Output the maximum integer value, e.g. "9223372036854775807"

Constant Arrays (PHP 7 and above)

In PHP 7 and above, constants can also be arrays.

Example

define("FRUITS", [
    "Apple",
    "Banana",
    "Orange"
]);

echo FRUITS[0]; // Output "Apple"

Or using const:

Example

const COLORS = [
    "Red",
    "Green",
    "Blue"
];

echo COLORS[1]; // Output "Green"
Other Extensions