The difference between const and define in PHP

Source: Internet
Author: User
Tags constant definition define null

Define section:
Macros can be used instead of constant values, and can be used instead of expressions, even code snippets. (Macros are powerful, but error-prone, so their pros and cons are quite controversial.) )

The syntax for a macro is:

Macro value #define Macro name
As a recommendation and a common practice for a broad range of programmers, macro names often use all uppercase letters.

Advantages of using Macros:

1) make the code more concise and clear

Of course, it depends on you taking a proper name for the macro. In general, the name of a macro is more important to have a clear and intuitive meaning, sometimes it is better to make it longer.

2) Convenient Code maintenance

The processing of macros is referred to as "preprocessing" during compilation. In other words, before the formal compilation, the compiler must first put the code appears in the macro, with its corresponding macro value substitution, this process is a bit of you I in the word processing software in the search and replace. So using a macro to express a constant in your code, in the final analysis, uses an immediate number and does not explicitly specify the type of the quantity.

Const section

The format for a constant definition is:

Const data type constant name = constant value;
Constants defined by const have data types, and constants that define data types make it easy for the compiler to perform data checks, which can cause errors in the program to be checked. Constants must specify a value at the beginning, and then, in future code, we are not allowed to change the value of this constant.

The difference between the two:
Allocation of memory space. Define the macro definition, the memory space will not be allocated, the compilation will be replaced in the main function, just a simple substitution, no checks, such as type, statement structure, that is, the macro definition constant is purely a placement relationship, such as # define NULL 0; The compiler always uses 0 instead of NULL when it encounters null it has no data type (see the Pre-Processing section or MSDN for a C-language book). Whereas const-defined constants have data types, constants that define data types make it easy for the compiler to check for data, which can cause errors in the program to be checked. So the difference between const and define is that the const definition constant excludes the unsafe between programs.

Source: >  

From for notes (Wiz)

The difference between const and define in PHP

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.