When you learn PHP, you usually encounter variables before you hit PHP's constants. For this reason, constants can either be very easy to grasp or leave you thinking "What on earth are they for?" In this PHP tutorial, we see how constants are a real boon for PHP developers on Ubuntu.
Syntax
Defining a constant in PHP takes different syntax from variables. Variable assignment in PHP was not very different from assignment in other programming languages. But constants are defined with the following structure:
define("<CONSTANT>", "<value>");The name of a constant, like any other PHP label, can consist of letters, numbers, and underscores. While you can begin the name with an underscore, you may not begin it with a number. This is due to how PHP evaluates the expression. Further, by convention, the names of constants are always capitalised. The following are valid names of constants:
define("FOO", "something borrowed");
define("BAR", "something new");
define("FOOBAR", "something old");
define("FOO_BAR", "something blue");
While you technically can sandwich a constant's main name between double underscores like PHP's magic variables, this is not recommended. As PHP remains under continued development, there is the likelihood that the language may change to include a magic constant of the same name or to evaluate your constant definition in a different way because of its similarity to magic constants.
Unchangeable
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.
Stable
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.
Global
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.
PHP Tutorials:
Apache Tutorials:
Install Apache | Basic Configuration | Apache In-Depth |
Recommend Web Hosting | Customising Apache | PHP Hosting |
Install MySQL | Basic MySQL Configuration | MySQL Configuration In-Depth |