深入分析PHP const与define使用区别
注意:define不能定义在类中,而const必须定义在类中,并且const必须通过类名::变量名来进行访问
1、const用于类成员变量定义,一旦定义且不能改变其值。define定义全局常量,在任何地方都可以访问。
2、define不能在类中定义而const可以。
3、const不能在条件语句中定义常量
4、const采用一个普通的常量名称,define可以采用表达式作为名称。
5、const只能接受静态的标量,而define可以采用任何表达式。
6、const 总是大小写敏感,然而define()可以通过第三个参数来定义大小写不敏感的常量
7.使用const简单易读,它本身是一个语言结构,而define是一个方法,用const定义在编译时比define快很多。
define是定义常量的,如果在类中定义常量呢?当然不能用define,而用const,如下例:
<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('copy7318')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7318><?php
//在类外面通常这样定义常量
define("PHP","jquerycn.cn");
class MyClass
{
//常量的值将始终保持不变。在定义和使用常量的时候不需要使用$符号
const constant = 'constant value';
function showConstant() {
echo self::constant . "<br>";
}
}
echo MyClass::constant . "<br>";
$classname = "MyClass";
echo $classname::constant . "<br>"; // PHP 5.3.0之后
$class = new MyClass();
$class->showConstant();
echo $class::constant."<br>"; // PHP 5.3.0之后
//print_r(get_defined_constants()); //可以用get_defined_constants()获取所有定义的常量
?>
一般是define在类外定义常量,const在类内定义常量,并且const必须通过类名::变量名来进行访问。但是php5.3以上支持类外通过const定义常量,看如下,这样是ok的:
<?php
//@blog<http://www.jiaochengji.com>
const a = "abcdef";
echo a;
?>
关于常量的基础知识,这里不说了,除了以上,define和const的其它区别(摘自网络):
1.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('copy7407')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7407><?php
if(1){
const a = 'java';
}
echo a; //必错
?>
2.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('copy7563')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7563>
<?php
const FOO = 'PHP';
for ($i = 0; $i < 32; $i) {
define('PHP_' . $i, 1 << $i);
}
?>
3.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('copy2033')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2033><?php
const PHP = 1 << 5; // 错误
define('PHP', 1 << 5); // 正确
?>
4.const本身就是一个语言结构。而define是一个函数。所以使用const速度要快的多。
两个共同步:两者都是不能进行重新赋值。
您可能感兴趣的文章:
深入分析PHP const与define使用区别
深入分析php中const和define定义常量的区别
php中const与define的区别分析
php中define()与const的区别详解
const define 区别PHP
php常量定义的方式有哪些
php中static,const与define的区别分析
PHP5.6 CONST新特性几个例子
详解PHP中const和define的区别
区别PHP中的const,static,public,private,protected