PHP const and define differences and usage examples

Source: Internet
Author: User
Tags constant definition define null scalar
The so-called constant is a simple identifier. The value cannot be changed during script execution. Constants are case sensitive by default. Usually, constant identifiers are always uppercase. Constants can only contain scalar data (Boolean, Integer, float, and string). You can define resource constants, but avoid them as much as possible, because they cause unpredictable results.

Define part
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 const and define:
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.

Using const makes the code easy to read, and const itself is a language structure, and define is a function. In addition, const is much faster to compile than define.

1, const for the definition of class member variables, once defined, can not be modified. Define cannot be used for definitions of class member variables and can be used for global constants.

2, the const can be used in the class, define cannot

3, const can no longer define constants in the conditional statement, the code is shown as follows:

<?phpif (...) {Const FOO = ' BAR ';    Invalid Invalid}if (...) {define (' FOO ', ' BAR ');//Valid valid}?>

4, the const uses the ordinary constant name, define may use the expression as the name, the code displays as follows:

<?phpconst  FOO = ' BAR '; for ($i = 0; $i <; + + $i) {define (' Bit_ '. $i, 1 << $i);}? >

5, const can only accept static scalar, and define can take any expression, the code is shown as follows:

<?phpconst bit_5 = 1 << 5;    Invalid Invaliddefine (' Bit_5 ', 1 << 5); The effective valid?>

6, const-defined constants are case-sensitive, and define can specify case sensitivity by a third argument (true for case insensitivity), and the code is shown as follows:

<?phpdefine (' FOO ', ' BAR ', true); Echo FOO; Barecho foo; Bar?>


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.