PHP define: description of constant names

It is always and everywhere possible to use only variables, but this is not the best solution to optimize the process of code execution and the logic of things. The constant components of the site should be moved to the static part, which has greater access speed, more protection from changes, including malicious ones.

Description of constants

A constant is described only once and only in the PHP define construct. A constant captures a name, not a value. The requirement that the value of the constant be constant remains in the distant past. Today, the designation of the parameter has come to the fore, and not its value.

It is convenient to name, for example, the root folder of a site, write, read or delete operations with human-readable names and pass such names as parameters to functions. In fact, numbers are transmitted that do not carry any meaning.

From the very beginning, constants were messages, mathematical coefficients, and integer values. Currently, client language localization requirements have automatically vetoed PHP define constants as error messages, dialog elements, and site content. It is impossible to fix what in one country does not look like in another.

Mathematical values ​​can be designated as constants. It remains, although rarely used. The specific parameters that it is desirable to protect for security reasons depend on the specific development and relate to the coding style, which restricts intentional harm by means of the programming language (PHP gives an error when re-declaring).





Features of constants




The main thing in using the PHP define construct ('Parameter Name', 'Value') is the first element. You can also specify the case sensitivity by the third parameter (True), but this is not essential. Programming, as a classic jurisprudence, began to select from the past experience only the most essential.

Features of constants

After the construct PHP define ('PMA_ENGINE_DETAILS_TYPE_PLAINTEXT', 0), you can use the meaningful name PMA_ENGINE_DETAILS_TYPE_PLAINTEXT, rather than the meaningless '0'. But passing as a parameter is easier, more convenient and more correct than a prime number.

Name / Value Relationship




The constant is not preceded by the $ symbol, it is used meaningfully and makes the script code readable and comfortable. There was a style of writing constants only in capital letters, separating the words with an underscore. However, this rarity from C / C ++ is increasingly fading into the background.

Constant is static




Define in PHP is a constant. It should be meaningful, concise and as compact as possible - this is the main thing. A constant is a static, which means you can think about how to use it once, then apply it effectively.




All Articles