教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php中define()与const的区别详解

php中define()与const的区别详解

发布时间:2016-10-21   编辑:jiaochengji.com
教程集为您提供php中define()与const的区别详解等资源,欢迎您收藏本站,我们将为您提供最新的php中define()与const的区别详解资源
在php中define()与const()都可以定义常量,那么define()与const的区别到底在哪里呢,这个很多程序员都不明白,下面我给大家介绍一些关于此函数用法比对吧。

define()与const的区别:

define() 在执行期定义常量,而 const 在编译期定义常量。这样 const 就有轻微的速度优势(即性能稍微好点),但不值得考虑这个问题,除非你在构建大高并发系统。

define() 将常量放入全局作用域,即使在命名空间中使用define方法定义常量也属于全局作用域的。不能使用 define() 定义类常量(类常量使用const定义),命名空间作用域内的常量使用const定义如: namespace const ABC=’100′;。

define() 允许你在常量名和常量值中使用表达式,而 const 则都不允许。 这使得 define() 更加灵活。
define() 可以在 if() 代码块中调用,但 const 不行。

在同一作用域下,define()常量名和const定义的常量名不能相同.
const可以定义类常量和命名空间常量. 如

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3156')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3156>

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

4、const采用一个普通的常量名称,define可以采用表达式作为名称。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6312')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6312>


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

5、const只能接受静态的标量,而define可以采用任何表达式。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9563')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9563>

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

6、const 总是大小写敏感,然而define()可以通过第三个参数来定义大小写不敏感的常量

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9493')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9493>

define('FOO', 'BAR', true);  www.jiaochengji.com

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

总结:
使用const简单易读,它本身是一个语言结构,而define是一个方法,用const定义在编译时比define快很多。

您可能感兴趣的文章:
const define 区别PHP
php中define()与const的区别详解
详解PHP中const和define的区别
php常量定义的方式有哪些
php中const与define的区别分析
PHP5.6 CONST新特性几个例子
深入分析PHP const与define使用区别
深入分析php中const和define定义常量的区别
区别PHP中的const,static,public,private,protected
php中static,const与define的区别分析

[关闭]
~ ~