Constant variable definition
The keyword const creates a read-only reference to the value. It can neither be re-declared nor can't a value be reassigned to it. That means a const variable or its value can't be changed in a program.
Now, let's see how to use or declare constant in some programming languages.
C programming language
In C, we can define the constant in two ways that are by using the const keyword or by using the #define preprocessor. Now, let's see the piece of codes to see how to define constant using both ways.
By using const keyword
The value of PI is: 3.140000
In the above code, the variable PI is constant, and on trying to change its value, an error will be rendered.
By using #define preprocessor
In the above code, the variable PI is constant and defined using the #define preprocessor.
A const variable cannot be hoisted, which means that a variable declared/initialized using var keyword cannot be reassigned using const.
The value of const variable x = 16
In PHP, there are two ways of defining a constant variable that is by using the define() function or by using the const keyword. Constants are similar to the variable except that we cannot change them. They remain constant across the program. Constants in PHP follows the rules same as the PHP variable. Now let's see the piece of codes to see how to define a constant in PHP by using the define() function or by using the const keyword.
By using the define() function
In the above code, the msg is the constant variable name, and Hello World is its value. If we try to modify its value, an error will be rendered.
By using the const keyword
PHP introduced a const keyword to create a constant. It is a language construct, not a function. The constant defined using the const keyword is case-sensitive.