The value of the constant is not changed during the execution of the program. It is case-sensitive. The constant identifiers are always uppercase. The name of the constant starts with letters, numbers, or underscore. We can define a constant but it cannot be changed or undefined.
The function define() is used to define a constant and by specifying its name you can retrieve the value of the constant. There is no need to have a constant with a ($) sign we can also use the function constant() to read a constant's value in case you want to obtain the contact's name dynamically.
By the name you can guess that it returns constantly as the value. It is stored in a variable and is returned by a function. Constants contain only scalar data Boolean, integer, float, and string).
Below are examples of some valid and invalid constant names.
Magic constants
These constants are case-insensitive and can change depending on where they are been used. There are five magical constants in PHP and are illustrated below.
Name |
Description |
__LINE__ |
Shows the current line number in the file |
__FILE__ |
Shows the full path and filename of the file. If we use this inside include, the included file name is returned. PHP 4.0.2, _FILE_ always contains an absolute path while it is not the case for older versions it contained a relative path under some circumstances. |
__FUNCTION__ |
Returns the function name as declared (case sensitive) in PHP 5 while In PHP 4 its value is always lowercased. |
__CLASS___ |
Returns the Class name as declared (case sensitive) in PHP 5 while In PHP 4 its value is always lowercased. |
__METHOD__ |
Returns the Method name as declared (case sensitive) in PHP 5. |