Examples
The value of PI is: 3.14
The current PHP version is: 8.1.30
In PHP, constants are like variables, Their values cannot be changed or redefined once they are set. How they differ from variables:
- Constants: Defined using the
define()
function, constants hold a constant value throughout the script execution. Once defined, their value cannot be altered.
- Variables: Variables can hold different values throughout the script they can be reassigned or modified using operators.
- Constants are used to store values that are not expected to change, such as configuration settings, version numbers, and mathematical constants.
- Constants can make your code more readable and maintainable by providing meaningful names for values that are used frequently.