Constants in PHP

Admin   PHP   807  2020-09-18 02:14:43

A constant is a name or an identifier for a single value. The value of the constant cannot be changed during script execution. By default, a constant is case sensitive. By convention, constant identifiers are always uppercase. Constant names begin with a letter or an underscore(_), followed by any number of letters, numbers, or underscores. If you have defined a constant, it can never be changed or redefined.

To define a Constant in PHP, you must use the define() function and to get the value of a constant, you must specify its name. Unlike variables, you don't need to add $ to a constant. You can also use the function constant () to read a constant's value if you want to get the name of the constant dynamically.

The function constant() in PHP

As the function name mentioned, this function will return the value of the constant.

This is useful when you want to get the value of a constant, but you don't know its name, for example it is stored in a variable or returned by a function.

Example of function constant() in PHP

   define("BLOGDEV", 98);
   echo BLOGDEV;
   echo constant("BLOGDEV");

Only scalar data (Boolean, integer, float, and string) can be kept in constants.

The difference between constants and variables in PHP is:

To define a variable need to write the character $ first, otherwise constant is not needed.

Constants cannot be defined using simple assignment, they can only be defined using the define () function.

Constants can be defined and accessed anywhere regardless of the variable scoping rule.

A constant, once defined, cannot be redefined or undefined.

Valid and invalid constant names in PHP

// Valid constant name example
define("ONE",     "Valid constant name example");
define("TWO2",    "Valid constant name example");
define("THREE_3", "Valid constant name example")
// For example invalid constant names

define("2TWO",    "For example invalid constant names");
define("__THREE__", "For example invalid constant names");

Magic constants in PHP

PHP provides a large number of predefined constants so that any script can use it.

There are 5 magic constants that change depending on where they are used. For example, the value of __LINE__ depends on which line it is used in your script. These special constants are case sensitive.

The table below lists some magic constants in PHP:

Name Describe
__LINE__ The current line of the file
__FILE__ The full path and full name of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path, while in older versions they contain relative paths in some cases.
__FUNCTION__ Name of the function. (Added in PHP 4.3.0) As of PHP 5, this constant returns the name of the function as it was declared (case sensitive). In PHP 4 its value is always lowercase
__CLASS__ Name of the class. (Added in PHP 4.3.0) As of PHP 5, this constant returns the name of the class as it was declared (case sensitive). In PHP 4 its value is always lowercase
__METHOD__ The name of the class method. (Added in PHP 5.0.0) This method name is returned as declared (case sensitive).