- PHP constants are identifier or name that can’t be changed during the execution of the script except for magic constants, which are not really constants.
- Constants can never be undefined or changed, they are similar to the variable except once defined.
- PHP constants follow the same PHP variable rules and it remains constant across the entire program.
- Conventionally, in uppercase letters PHP constants should be defined and it can be started with an underscore or letter only.
- PHP constants consists of two types, they are:
- Using define () function
- Using const keyword
Using define () function
- In PHP use the define () function to create a constant and it defines constant at run time.
- In this function name specifies the constant name, value specifies the constant value and case-insensitive specifies whether a constant is case-insensitive.
- By default, if value is false it means it is case sensitive.
Sample Code
Output
Using const Keyword
- PHP introduces a const keyword to create a constant and it defines constants at compile time.
- It constructs a language, not a function.
- The constant defined using const keyword are case-sensitive.