A constant is a name or an identifier for a simple fixed value. Constants are called so because once they are defined, they cannot be changed during the course of script execution.
Due to their global scope PHP constants are usually used to store data that needs to have a global scope or whose value is bound to be constant.
Naming Conventions for PHP Constants
Constant names and variable names have similar naming conventions which can be summarized as follows:
- A valid constant name starts with a letter or an underscore, followed by any number of letters, numbers or underscores.
- The $ prefix is not required for constant names.
- Once a constant is defined, it can never be changed or undefined.
Eg: Configuration settings such as database username and password, company name are defined as constants because their values will remain constant during the course of the program.
Some examples of valid constant names are as follows :
Some examples of invalid constant names are as follows :
Define() Function
We can use the define() function to define a constant for which the syntax is :
define(constant name, value , case-sensitivity)
In which the constant name parameter is the name of the constant whereas the case-sensitivity parameter defines whether the constant is case-sensitive or not.
Eg: define("php_message", "Learn PHP because PHP is fun!", true);
Constant() function
We can retrieve the value of a constant by simply specifying its name. The constant() function is used in order to get the value of a constant or retrieve the constant name dynamically.
constant() example
Differences between constants and variables
- There is no need to specify a dollar sign ($) before a constant, but a variable name should always be preceded by the $ symbol.
- Constants cannot be defined using a simple assignment like a variable because they can only be defined using the define() function
- They can be defined and accessed anywhere without variable scoping rules because they only have a global scope
- Constants are so called because once they have been defined they cannot be re-defined or undefined.
PHP Magic Constants
PHP has a large number of predefined constants which are called magic constants. They are called magic constants because they are pre-defined by default in any script and because the programmer can access their values at any time in the script.
There are five magic constants whose values change due to the position where they are used. For eg, the value of '__LINE__' depends on the line that it's being used on in your PHP code.
Magic constants are case-insensitive hence they can be used without caution. Some of the magic constants which are commonly used are as follows:
- __LINE__ - Contains the number of the lines in the PHP file which is currently being executed.
- __FILE__ - Contains the name of the PHP script file, which is currently being executed.
- __FUNCTION__ - Has the name of the PHP function which is currently being executed.
- __CLASS__ - Contains the class name which is currently in use
- __METHOD__ - Contains the name of the method which is currently executing in the current class