Once a constant value is defined, it cannot be changed anywhere else in the script.
A constant is an identifier for a simple value. This value cannot be changed in the script.
A constant consists of English letters, underscores, and numbers, but numbers cannot appear as the first letter. (The constant name does not require the $ modifier).
Note: Constants can be used throughout the script.
To set constants, use the define() function. The function syntax is as follows:
bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )
This function has three parameters:
name: required parameter, constant name, i.e. identifier.
value: required parameter, value of constant.
case_insensitive : Optional parameter, if set to TRUE, this constant is case-insensitive. The default is case-sensitive.
In the following example, we create a case-sensitive constant with the value "Welcome to codecto.com":
<?php // Case-sensitive constant names define ( " GREETING " , " Welcome to codecto.com " ) ; echo GREETING ; // Output "Welcome to codecto.com" echo ' <br> ' ; echo greeting ; // Output "greeting" ?>
In the following example, we create a case-insensitive constant with the value "Welcome to codecto.com":
<?php // Case-insensitive constant name define ( " GREETING " , " Welcome to codecto.com " , true ) ; echo greeting ; // Output "Welcome to codecto.com" ?>
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 the use of constants within a function. Even if the constant is defined outside the function, the constant can be used normally.
<?php define ( " GREETING " , " Welcome to codecto.com " ) ; function myTest ( ) { echo GREETING ; } myTest ( ) ; // Output "Welcome to codecto.com" ?>