1、const用于类成员变量定义,一旦定义不能改变值。define定义全局常量,在任何地方都可以访问。
2、define不能在类中定义而const可以。
3、const不能在条件语句中定义常量
if (...) {
const FOO = 'hello'; // 无效
}
but
if (...) {
define('FOO', 'hello'); // 有效
}4、const采用一个普通的常量名称,define可以采用表达式作为名称。
const FOO = 'hello';
for ($i = 0; $i < 32; ++$i) {
define('hello' . $i, 1 << $i);
}5、const只能接受静态的标量,而define可以采用任何表达式。
const hello= 1 << 5; // 无效
但是
define('hello', 1 << 5); // 有效6、const 总是大小写敏感,然而define()可以通过第三个参数来定义大小写不敏感的常量
define('hello', 'hello', true);
echo hello; // hello总结:
使用const简单易读,它本身是一个语言结构,而define是一个方法,用const定义在编译时比define快很多。
上一篇: 没有了
下一篇: Mysql联合查询...