Home  >  Article  >  Backend Development  >  Two ways to define constants in php

Two ways to define constants in php

angryTom
angryTomOriginal
2019-08-22 13:36:4512826browse

Two ways to define constants in php

The broad concept of "constant" is: 'a quantity that does not change' (for example: a quantity that will not be modified by the program when the computer program is running; a certain quantity in a mathematical function , such as the radius and diameter of each specific circle; the gravitational acceleration close to the ground in physics; the speed of light in vacuum;) Here are two ways to define constants in PHP.

Recommended tutorial: PHP video tutorial

We use pi as an example here.

The first way: Macro definition is to define Pi as a symbol. At this time, Pi is just an alias of 3.1415926, and 3.1415926 is used during compilation. to replace the value of Pi.

 1.define is a macro definition, and the program will replace it with the content defined by define during the preprocessing stage. Therefore, when the program is running, there are no constants defined with define in the constant table, and the system does not allocate memory for it. Constants defined by const are in the constant table when the program is running, and the system allocates memory for it.

 2. The constants defined by define are just replaced directly during preprocessing. Therefore, data type checking cannot be performed at compile time. Constants defined by const undergo strict type checking at compile time to avoid errors.

 3. When defining an expression, pay attention to the "edge effect", for example, the following definition: #define N 2 3 //Our expected N value is 5, we use N like this, int a = N/2 ; //We expected the value of a to be 2.5, but in fact the value of a is 3.5. The reason is that during the preprocessing stage, the compiler processed a = N/2 into a = 2 3/2; this is the macro definition The "edge effects" of string substitution are therefore defined as follows: #define N (2 3).

The second way is to define PI as a variable, but tell the compiler that its value is fixed , if you try to modify it in the program Its value will report an error during compilation.

Constants defined by const are called constant variables for two reasons: const-defined constants check the type like variables; const can define constants anywhere, and the compiler treats it similarly to variables, except where memory is allocated different.

 To sum up, it is recommended to use const instead of #define preprocessing instructions(But currently I see more people using #define. Is it convenient? ? To be considered)

 1. const can define data types, improving type safety. For example, above we can specify that the constant PI is of type double

 2. Since const is a variable (it is a bit awkward here, it is actually unchanged, but the name is called a constant variable), then there is an address, which is applicable Wider coverage

 3. The grammar is also better understood

符号常量 #define Pi 3.1415926f;
 
常值变量 const float pi 3.1415926f;

The above is the detailed content of Two ways to define constants in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn