PHP_Const, constphp

Source: Internet
Author: User

PHP_Const, constphp

PHP_Const

Constant rules:
1 is always capitalized
2 A-Z and from 127 ~ 255 ASCII characters
3. global scope
4. define with define function
5. Only scalar data can be contained, such as Boolean integer float string.
6. Do not add a dollar sign before

PHP built-in constant = Special constant
Case Insensitive
Current row number in the _ LINE _ file
_ FILE _ complete FILE path + FILE name
_ FUNCTION name
_ CLASS Name
_ METHOD _ class METHOD name

_ LINE _
Php script number of rows. If a file is referenced, the constant in the referenced file is the row of the referenced file.
Instead of passing the row of the referenced file downward

_ FILE _
Same principle

 

Define section:
Macros can be used not only to replace constant values, but also to replace expressions or even code segments.

(Macros have powerful functions but are prone to errors, so their advantages and disadvantages are quite controversial .)


Macro Syntax:
# Define macro name macro value
As a suggestion and a common habit of programmers, macro names often use uppercase letters.


Advantages of macros:
1) make the code more concise and clear
Of course, this depends on a proper name for the macro. Generally, the macro name should be more explicit and intuitive, and sometimes it is better to keep it longer.
2) Easy code Maintenance
Macro processing is called "preprocessing" during compilation ".

That is to say, before official compilation, the compiler must replace the Macros in the Code with their corresponding macro values. This process is a bit like searching and replacing in the Text Processing Software. Therefore, when macro expressions are used in the Code, the immediate number is used in the final analysis, and the type of this number is not explicitly specified.

Const Section
Constant Definition Format:
Const data type constant name = constant value;

A constant must be specified at the beginning. Then, in future code, we cannot change the value of this constant.

Differences between the two:
1. memory space allocation.

When define is used for macro definition, the memory space is not allocated. during compilation, the memory space is replaced in the main function, but the memory space is replaced without any check,

For example, type, statement structure, etc. That is, macro-defined constants are purely placement relationships, for example, # define null 0;

When the compiler encounters null, it always uses 0 instead of null. It has no data type. (For more information, see C language books for preprocessing or MSDN.

A constant defined by const has a data type,

Defining constants of the data type is convenient for the compiler to check the data, so that the program may encounter Errors for troubleshooting,

So the difference between const and define is that

Const defines constants to eliminate security between programs.

Define defines global constants and can be accessed anywhere

Const is used to define class member variables. It can only be accessed by class names and cannot be changed. If you are a beginner, you must first understand it.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.