A simple value can be referred to by its name, which is known as a constant. During the course of the script\’s execution, a constant value will not undergo any changes. Case sensitivity is turned on for a constant by default. Constant identifiers are usually written in uppercase because this is the practise. The first character of a constant name can be a letter or an underscore, and it is then followed by any combination of letters, digits, and underscores. When a constant has been defined, it cannot subsequently be altered or undefined under any circumstances.
To define a constant, you will need to use the define() function, and to retrieve the value of a constant, you will only need to supply its name. Using define() will allow you to define a constant. In contrast to variables, it is not necessary to preface a constant with a dollar sign. If you want to retrieve the name of the constant in a dynamic manner, you may also use the function constant() to read a constant\’s value if you want to do so.
This function will, just as its name suggests, return the value that has been set for the constant.
When you want to obtain the value of a constant but you do not know its name, for example because it is stored in a variable or returned by a function, you can leverage this to your advantage.
constant() example
<?php
define(\”MINSIZE\”, 50);echo MINSIZE;
echo constant(\”MINSIZE\”); // same thing as the previous line
?>
Constants are only able to store scalar data, which includes boolean, integer, floating point, and string values.
The main Differences between constants and variables are as follows:
Before writing the value of a constant, you do not have to put the dollar sign ($), but before writing the value of a variable, you must use the dollar sign.
It is not possible to define a constant by simply assigning a value to it; rather, the define() function is required in order to do so.
Without respect to the rules that govern the scope of variables, constants can be defined and retrieved anywhere.
After they have been established as constants, they cannot be redefined or undefined again.
Valid and Invalid names for constants
// Valid constant names
define(\”PERSON\”, \”first value\”);
define(\”MEN\”, \”second value\”);
define(\”WOMEN_3\”, \”third value\”);
define(\”__OTHER__\”, \”third value\”);// Invalid constant names
define(\”2PERSON\”, \”second value\”);
PHP Magic constants
Any script that PHP is used to run receives access to PHP\’s extensive library of predefined constants.
There are five magical constants, each of which takes on a different form depending on the context in which it is utilised. For instance, the value of the __LINE__ variable changes depending on the line in your script where it is utilised. These special constants do not differentiate between upper and lower case and are as follows:
The following are examples of a few \”magical\” PHP constants:
- __LINE__
The line number that is currently being used in the file. - __FILE__
The complete directory path as well as the filename of the file. If this expression is used inside of an include, the name of the file that is being included is returned. Since version 4 of PHP, the __FILE__ variable has consistently been set to hold an absolute path, although in earlier versions of the language it occasionally held a relative path. - __FUNCTION__
The name of the function. (Added inversion 4 of PHP ) Beginning with PHP version 5, this constant returns the function name exactly as it was declared (case-sensitive). In PHP 4, the value of this variable is never capitalised. - __CLASS__
The name of the class. (New in version 4 of PHP) This constant, available since PHP 5, returns the class name exactly as it was declared (case-sensitive). In PHP 4, the value of this variable is never capitalised. - __METHOD__
The name of the class method. (New in version 5.0.0 of PHP) The name of the method is given back exactly how it was declared (case-sensitive).