Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP Constants

PHP Constants


A constant in PHP is an identifier whose value cannot be changed once it is defined.

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

A constant can be defined using the define() function or the const keyword.

Characteristics 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 global The const keyword.
  3. Data Type: 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 can set define() the third parameter of the function to true.

A constant is an identifier for a simple value that cannot be changed during script execution.

A constant consists of English letters, underscores, and digits, but a digit cannot appear as the first character. (A constant name does not need the $ prefix.)

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 ] )

This function has three parameters:

In the example below we create a case-sensitive constant(not recommended after PHP 7.3), whose value is "Welcome to Runoob.com":

Example

' ; echo greeting ; // output "greeting", but there is a warning indicating the constant is undefined ?>

In the example below we create a case-insensitive constant, whose value is "Welcome to Runoob.com":

Example


Constants Are Global

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

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

Example


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.runoob.com"

;

echo

SITE_URL

;

// output "https://www.runoob.com"


Predefined Constants

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

Example

echo

PHP_VERSION

;

// output the PHP version, e.g. "7.4.1"

echo

PHP_OS

;

     

// output the operating system, e.g. "Linux"

echo

PHP_INT_MAX

;

// output the largest 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 use const:

Example

const

COLORS

=

[

   

"Red"

,

   

"Green"

,

   

"Blue"

]

;

echo

COLORS

[

1

]

;

// output "Green"