The value of constants can sometimes be lost when you learn PHP the first time through. In this PHP tutorial, we look at some of the characteristics that give PHP constants their value to developers. The attributes covered below are:
As we saw, PHP variables are called such because of their variable content. PHP constants are so called for the same reason. Once defined, constants cannot be changed. They are immutable.
Most software is not written by one person. Modern applications are so complicated that many would take several lifetimes for one person to code, test, troubleshoot, debug, and release. Whether you use agile development, one of the most common paradigms used today, or another pattern for software engineering, the odds are that somebody else down the line will be revising your code, even if they are not extending it. Constants give you control over your code in these circumstance and help mitigate against errors being introduced by someone changing a value on which your code depends. This leads to the next characteristic of constants: stability.
The virtue of an unchanging value is that it can be used as a pillar to support other parts of your program. You can build on it and know that no module is going to be allowed to alter the value. This can be for the seed of an algorithm or for a simple value check.
PHP constants are global in their scope. They can be referenced anywhere in an executed stream. So if you build a module to work with another PHP file, you can reference that file's constant's without worrying about their scope. They are always available.
|Install PHP||Basic PHP Configuration||PHP Configuration In-Depth|
|Learn PHP||PHP in HTML||PHP Keywords|
|PHP Expressions||PHP Variables||PHP Constants|
|Defined Constants||Constants Characteristics||PHP Datatypes|
|PHP Syntax Basics||PHP Loops|
|Install Apache||Basic Configuration||Apache In-Depth|
|Recommend Web Hosting||Customising Apache||PHP Hosting|
|Install MySQL||Basic MySQL Configuration||MySQL Configuration In-Depth|