Differences between const and define in php _ PHP Tutorial

Source: Internet
Author: User
Parse the differences between const and define in php. 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. 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.

The code is as follows:


If (...){
Const FOO = 'bar'; // invalid
}
But
If (...){
Define ('foo', 'bar'); // valid
}


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

The code is as follows:


Const FOO = 'bar ';
For ($ I = 0; $ I <32; ++ $ I ){
Define ('bit _ '. $ I, 1 <$ I );
}


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

The code is as follows:


Const BIT_5 = 1 <5; // invalid
But
Define ('bit _ 5', 1 <5); // valid


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

The code is as follows:


Define ('foo', 'bar', true );
Echo FOO; // BAR
Echo foo; // BAR


Summary:
Const is easy to read. it is a language structure, and define is a method. it is much faster to define const in compilation than define.

Bytes. Define defines global constants that can be accessed anywhere. 2. define cannot be defined in the class, but const can ....

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.