PHP Constants
A single value identifier is referred to as a constant (name). It cannot change the value while the script is running.
A valid constant name begins with a letter or underscore (there is no $ sign preceding the constant name).
Note :- Contrary to variables, constants automatically reach the whole script globally.
Create a PHP Constant
Use the define()
method to build a constant.
Syntax :-
define(name, value, case-insensitive).
Parameters :
- name: Specifies the name of the constant.
- value: Specifies the value of the constant.
- case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false.
Exampe 1 :- Create a constant with a case-sensitive name.
Output :-
Example 2 :- Create a constant with a case-insensitive name:
Output :-
Related Links
Constants are Global
Constants are globally automated and can be used throughout the script.
Example :- This example utilizes a function constantly, even though it is specified outside of the function :
Output :-
Related Links
PHP Constant Arrays
The define()
method in PHP7 allows you to build an Array constant.
Example :- Create an Array constant:
Output :-