Constant in PHP-
- A constant is an identifier for holding a value that can not be changed during program or script execution.
- The constant value cannot change during the execution of the program or script Â (except for magic constants, which aren't actually constants).
- In PHP a constant is case-sensitive by default.
- Constant identifiers name should Â always uppercase by convention but not mandatory.
- The constant name declaration follow the same rules as any label in PHP. A valid constant name begin with a letter or underscore, followed by any number of letters, numbers, or underscores.
Creating a PHP Constant-
To create a constant, use the define() function.
define(name, value, case-insensitive)
name: Refers the name of the constant
value: Refers the value of the constant
case-insensitive: Refers whether the constant name should be case-insensitive. Default is false
The following example demonstrate a constant with a case-sensitive name -
// Valid constant names
define("MESSAGE", "Data saved successfully.");
define("ERROR_MESSAGE", "Error! data could not be saved.");
Note- The scope of a constant is global. You can access constants anywhere in your script without regard to scope.