Home >Backend Development >PHP Tutorial >PHP_Const
PHP_Const
Constant rules:
1 Always uppercase
2 A-Z and ASCII characters from 127 to 255
3 Global scope
4 Define with define function
5 Can only contain scalar data such as Boolean integer float string
6 No dollar sign in front Symbol
PHP comes with constants = special constants
Case-insensitive
_LINE_ Current line number in the file
_FILE_ Full path to the file + file name
_FUNCTION_ Function name
_CLASS_ Class name
_METHOD_ Method name of the class
_ LINE_
php script If a file is referenced, the constant in the referenced file is the line of the referenced file. The line of the non-referenced file is passed down. _FILE_
The principle is the same as above.
Macros can not only be used to replace regular Numeric values can also be used to replace expressions or even code segments. (Macros are very powerful, but they are also error-prone, so their pros and cons are quite controversial.)
The syntax of macros is: #define macro name macro value
As a suggestion and a common habit among programmers, macros Names often use all capital letters.
Use the advantages of macros:
1) Make the code more concise and clear
Of course, this depends on you giving the macro an appropriate name. Generally speaking, macro names should have clear and intuitive meanings, and sometimes it is better to make them longer. 2) Convenient code maintenance
The processing of macros is called "preprocessing" during the compilation process. That is to say, before formal compilation, the compiler must first replace the macros that appear in the code with their corresponding macro values. This process is similar to the search and replace you and I use in word processing software. Therefore, when macros are used to express constants in the code, in the final analysis, immediate numbers are used, and the type of this quantity is not clearly specified.
const part
The format of constant definition is:
const data type constant name = constant value;The constants defined by const have data types. Constants defining data types facilitate the compiler to check data, so that errors may occur in the program Conduct troubleshooting. A constant must initially specify a value, and then, in subsequent code, we are not allowed to change the value of this constant.
The difference between the two:
within
On the allocation of storage space. When define defines a macro, it will not allocate memory space. It will be replaced in the main function during compilation. It is just a simple replacement without any checks, such as type, language.
Sentence structure, etc., that is, macro definition constants are just pure placement relationships, such as #define null
0; The compiler always replaces null with 0 when it encounters null. It has no data type (if you have any questions, please look at the preprocessing part of the C language book or MSDN. The constants defined by const have data types.
Type, defining constants of data types facilitates the compiler to check data and troubleshoot possible errors in the program. Therefore, the difference between const and define is that const defines constants to exclude the program.
The insecurity between. define defines global constants and can be accessed anywhere
const is used for class member variable definitions. It can only be accessed with the class name and cannot be changed. If you are a beginner, this is
Just understand clearly and don't be too arrogant.
A lot of object-oriented ideas have been added to PHP5. The object-oriented ideas of PHP5 are closer to the object-oriented ideas of Java. Here we will discuss the functions of static and const keywords in PHP5.
Let me describe it, I hope it will be helpful to friends who are learning PHP5.
The above has introduced PHP_Const, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.