Difference between phpconst and define, phpconstdefine_PHP tutorial

Source: Internet
Author: User
Difference between phpconst and define, phpconstdefine. Difference between phpconst and define, phpconstdefine original address: www.manongjc.comarticle491.html const is used for class member constant definition, cannot be changed after definition, and define we set the difference between php const and define, phpconstdefine

Address: http://www.manongjc.com/article/491.html

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, here are some details for you.

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, the value cannot be changed. Define defines global constants that 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 get 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 through const outside the class, as shown below, it is OK:

 

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

 

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

 

4. const itself is a language structure. Define is a function. Therefore, using const is much faster.

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

Http://www.bkjia.com/PHPjc/1102851.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1102851.htmlTechArticlephp const and define difference, phpconstdefine original address: http://www.manongjc.com/article/491.html const is used for class member constant definition, After definition can not be changed, and define we set...

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.