The difference between const and define in PHP

Source: Internet
Author: User

When defining constants in PHP, you can use both the const and define methods, so what's the difference between them?

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

2.const can be used in a class, define cannot.

3.const You cannot define constants in a conditional statement.

such as: if (constition) {

The philosophy can not use const, can only use define

}

4.const can only accept static scalars, and define can take any expression.

Const BIT_5 = 1 << 5; Invalid
Define (' Bit_5 ', 1 << 5); Valid

5.const defines a constant case-sensitive, while define can specify case sensitivity by using the third argument (true for case insensitivity).

The 6.const takes a common constant name. Define can use expressions as constants.

7. Using const makes the code easy to read, and const itself is a language structure. and define is a function.

8.const is much faster at compile time than define.

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.