Php framework-what is the difference between definevsconst and defined constant in PHP?

Source: Internet
Author: User
What are the differences between them in determining one variable? {Code...} performance or other aspects? What are the differences between them in determining one variable?

define('SOMETHING', true);andconst SOMETHING = true;

Performance or other?

Reply content:

What are the differences between them in determining one variable?

define('SOMETHING', true);andconst SOMETHING = true;

Performance or other?

Baidu can solve the problem. Is the landlord not self-taught?

[Question] What is the difference between const and define when defining constants in php? [Answer] Using const makes the code simple and easy to read. const itself is a language structure, and define is a function. In addition, const is much faster than define during compilation. (1). const is used for defining class member variables. Once defined, it cannot be modified. Define cannot be used to define class member variables and can be used as global constants. (2). const can be used in the class, and define cannot. (3). const cannot define constants in condition statements. Example: if (...) {const FOO = 'bar'; // invalid} if (...) {define ('foo', 'bar'); // valid} (4 ). const uses a normal constant name, and define can use an expression as the name. 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. For example, const BIT_5 = 1 <5; // invalid define ('bit _ 5', 1 <5); // valid (6 ). the constants defined by const are case sensitive, while define specifies whether the values are case sensitive by using the third parameter (true indicates case insensitive. Example: define ('foo', 'bar', true); echo FOO; // BAR

From: PHP constant explanation: difference between define and const

Related 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.