The difference between const and define in PHP

Source: Internet
Author: User

1. Const is used for class member variable definition, once defined and cannot change its value. Define defines global constants that can be accessed everywhere.

2. Define cannot be defined in a class and const can.

3. Const cannot define constants in conditional statements

if (... {     const FOO = ' BAR ';    // if (.....    ) {     define//

4, const uses a common constant name, define can use the expression as the name.

Const  FOO = ' BAR ' for ($i$i < + +$i)     {define$ I$i

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

Const Bit_5 = 1 << 5;    //   define// 

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

Define true);  Www.2cto. com Echo //  echo//


Summarize:
The use of const is simple and easy to read, it is itself a language structure, and define is a method, with a const definition at compile time much faster than define.

<?  class  animal {    const type= ' Person1 ';     function Call ()     {        echo self::type;    }} Define (' name ', ' Zhaoxingzhuang '); $person New animal (); Call ( $person); Echo ' <br/> '; Echo name;? >

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.