Home >Backend Development >PHP Tutorial >Detailed explanation of the difference between define() and const in php_PHP tutorial

Detailed explanation of the difference between define() and const in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:50:08824browse

In PHP, both define() and const() can define constants. So what is the difference between define() and const? Many programmers don’t understand this. Let me introduce to you some usage comparisons of this function. Right.

The difference between define() and const:

define() defines constants at execution time, while const defines constants at compile time. There is a slight speed advantage to const (i.e. slightly better performance), but it's not worth considering unless you're building a heavily concurrent system.

define() puts the constant into the global scope. Even if the constant is defined using the define method in the namespace, it still belongs to the global scope. You cannot use define() to define class constants (class constants are defined using const), and constants within the namespace scope are defined using const, such as: namespace const ABC=’100′;.

define() allows you to use expressions in constant names and constant values, while const does not allow either. This makes define() more flexible.
define() can be called within an if() block, but const cannot.

In the same scope, the define() constant name and the constant name defined by const cannot be the same.
const can define class constants and namespace constants. Such as

namespace abc; const ABC = ‘a’; class hello { const C_NUM = 8; }

The code is as follows
 代码如下 复制代码

if (...) {
    const FOO = 'BAR';    // invalid
}
 
but
 
if (...) {
    define('FOO', 'BAR'); // valid
}

Copy code

 代码如下 复制代码


const  FOO = 'BAR';
 
for ($i = 0; $i < 32; ++$i) {
    define('BIT_' . $i, 1 << $i);
}

if (...) {

Const FOO = 'BAR'; // invalid

}
 代码如下 复制代码

const BIT_5 = 1 << 5;    // invalid
 
but
 
define('BIT_5', 1 << 5); // valid

but

 代码如下 复制代码

define('FOO', 'BAR', true);  www.bKjia.c0m

echo FOO; // BAR
echo foo; // BAR

if (...) {

Define('FOO', 'BAR'); // valid
}

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

The code is as follows
Copy code
const FOO = 'BAR'; for ($i = 0; $i < 32; ++$i) { define('BIT_' . $i, 1 << $i); } 5. const can only accept static scalars, while define can use any expression.
The code is as follows Copy code
const BIT_5 = 1 << 5; // invalid but define('BIT_5', 1 << 5); // valid 6. const is always case-sensitive, but define() can define case-insensitive constants through the third parameter
The code is as follows Copy code
define('FOO', 'BAR', true); www.bKjia.c0m echo FOO; // BAR echo foo; // BAR Summary: Using const is simple and easy to read. It is a language structure in itself, while define is a method. Using const to define is much faster than define at compile time. http://www.bkjia.com/PHPjc/632646.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632646.htmlTechArticleBoth define() and const() can define constants in php, so what is the difference between define() and const? Where is it? Many programmers don’t understand this. Let me introduce you to something about this function...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn