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