In-depth analysis of differences between PHPconst and define

Source: Internet
Author: User
Const is used for the definition of class member constants. it cannot be modified after definition, and define is used to define global constants, so that we can access but cannot change it elsewhere, we will list the details below. note: define cannot be defined in... const is used for the definition of class member constants. it cannot be modified after definition, and define is used to define global constants, so that we can access but cannot change it elsewhere, we will list the details below.

Note:Define cannot be defined in the class, and const must be defined in the class, and const must be accessed through the class name: variable name.

1. const is used for defining class member variables. once defined, define defines global constants and can be accessed anywhere.

2. define cannot be defined in the class but const can.

3. const cannot define constants in condition statements.

4. const uses a normal constant name, and define can use an expression as the name.

5. const can only accept static scalar, while define can use any expression.

6. const is always case sensitive. however, define () can use the third parameter to define a case-insensitive constant.

7. Using const is simple and easy to read. it is a language structure, and define is a method. Using const to define it is much faster than define during compilation.

Define defines constants. what if a constant is defined in a class? Of course, define cannot be used, but const can be used, for example:

 ";}} Echo MyClass: constant ."
"; $ Classname =" MyClass "; echo $ classname: constant ."
"; // $ Class = new MyClass (); $ class-> showConstant (); echo $ class: constant ."
"; // PHP 5.3.0 and later // print_r (get_defined_constants (); // you can use get_defined_constants () to obtain all defined constants.

Generally, define defines constants outside the class. const defines constants within the class, and const must be accessed by class name: variable name. However, php5.3 and above support defining constants outside the class through const, as shown in the following figure, it is OK:

      const a = "abcdef";     echo a;  

I will not talk about the basic knowledge of constants here. apart from the above, other differences between define and const (from the network ).

1. const cannot define constants in condition statements, but define is acceptable, as follows:

 

2. const uses a normal constant name, and define can use an expression as the name. The code is as follows:

 

3. const can only accept static scalar, and define can use any expression. the code is as follows:

 

4. const itself is a language structure, and define is a function, so using const is much faster.

Two common steps: Both of them cannot be re-assigned.

Tutorial URL:

You are welcome to add your _ favorites to the Favorites folder, but please keep the link for this article.

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.