In programming, a constant is a value whose value cannot change during the execution. Unlike variables, a PHP constant doesn’t have a $ sign before its name. A constant name can start with a letter or underscore. All constants have a global scope. This means that constants can be used across the entire script.

Creating a PHP constant

define function

To declare a constant, you have to use the define() function. This function takes the name of the constant as the first parameter and the value of the constant as the second parameter.

define("PI", 3.14);

By default, a constant name is case-sensitive. The third parameter of the define function determines whether the constant name should be case-sensitive or not. The default value is false.

The following example shows how to create a case-insensitive constant.

define("PI", 3.14, true);

Accessing the value of a constant

You can access the value of a constant simply by specifying it’s name.

Example

<?php
define("PI", 3.14); //case sensitive
define("Name", "John"); //case insensitive
echo PI;
echo name;
?>