A constant is a fundamental value's name or identifier. During the execution of the script, a constant value cannot change. A constant is case-sensitive by default, and constant identifiers are always in uppercase by convention. A constant name begins with a letter or underscore and continues with any number of letters, digits, or underscores. The same can never be changed or explained once it has been defined.
You must use the define() method to define a constant, and you merely give its name to obtain its value. You don't need a constant with a $ as you do with variables. If you want to get the name of a constant dynamically, you may use the function constant() to retrieve its value.
The function's name implies that it will return the value of the constant.
This comes in handy when you need to obtain the value of a constant but don't know its name, such as when it's stored in a variable or returned by a function.
Constants can only hold scalar data (boolean, integer, float, and string).
Any script that runs on PHP has access to many predefined constants.
Five magical constants vary depending on their application. The value of __LINE__, for example, is determined by the line on which it is used in your script. The following are the case-insensitive special constants:
Below are a few "magical" PHP constants:
Sr.No | Name & Description |
---|---|
1 | __LINE__ The file's current line number. |
2 | __FILE__ The file's entire location and filename. If used within the merge, the inserted file name is restored. Since PHP 4.0.2, __FILE__ has always included an absolute path, although in previous versions it occasionally contained a relative path. |
3 | __FUNCTION__ PHP 4.3.0 introduced this feature.) As of PHP 5, this constant returns the function name as it was declared (case-sensitive) |
4 | __CLASS__ The name of the class. (This was added in PHP 4.3.0) This constant, as of PHP 5, returns the class name as it was declared (case-sensitive). Its value is always lowercase in PHP 4. |
5 | __METHOD__ The name of the class method. (This was added in PHP 5.0.0.) The name of the method is returned exactly as it was stated (case-sensitive). |
|