php的常量定义:const VS define
来源:互联网 发布:网络电影脱轨下载 编辑:程序博客网 时间:2024/04/25 22:13
As of PHP 5.3 there are two ways to define constants: Either using the const
keyword or using the define()
function:
const FOO = 'BAR'; define('FOO', 'BAR');
The fundamental difference between those two ways is that const
defines constants at compile time, whereasdefine
defines them at run time. What does that mean for you?
const
cannot be used to conditionally define constants. It has to be used in the outermost scope:if (...) { const FOO = 'BAR'; // invalid } // but if (...) { define('FOO', 'BAR'); // valid }
Why would you want to do that anyways? One common application is to check whether the constant is already defined:
if (!defined('FOO')) { define('FOO', 'BAR'); }
const
accepts a static scalar (number, string or other constant liketrue
,false
,null
,__FILE__
), whereasdefine()
takes any expression:const BIT_5 = 1 << 5; // invalid define('BIT_5', 1 << 5); // valid
const
takes a plain constant name, whereasdefine()
accepts any expression as name. This allows to do things like this:for ($i = 0; $i < 32; ++$i) { define('BIT_' . $i, 1 << $i); }
const
s are always case sensitive, whereasdefine()
allows you to define case insensitive constants by passingtrue
as the third argument:define('FOO', 'BAR', true); echo FOO; // BAR echo foo; // BAR
So, that was the bad side of things. Now let's look at the reason why I personally always useconst
unless one of the above situations occurs:
const
simply reads nicer. It's a language construct instead of a function and also is consistent with how you define constants in classes.As
const
s are language constructs and defined at compile time they are a bit faster thandefine()
s.It is well known that PHP
define()
s are slow when using a large number of constants. People have even invented things likeapc_load_constants()
andhidef
to get around this.const
s make the definition of constants approximately twice as fast (on development machines with XDebug turned on even more). Lookup time on the other hand does not change (as both constant types share the same lookup table):Demo.
Summary
Unless you need any type of conditional or expressional definition, use const
s instead of define()
s - simply for the sake of readability!
- php的常量定义:const VS define
- PHP中定义常量的区别,define() vs. const
- php中const与define定义常量的区别
- PHP中const和define()定义常量的细节区别
- php定义常量时const与define的区别
- PHP定义常量是,const和define的区别
- PHP中const和define()定义常量的细节区别
- PHP中const和define()定义常量的细节区别
- PHP 定义常量const和define的区别
- PHP中const和define()定义常量的细节区别
- PHP中const和define()定义常量的细节区别
- PHP常量定义,define()和const区别
- #define和const定义常量的区别
- php const和define的区别(php类中如何定义常量)
- php const和define的区别(php类中如何定义常量)
- php const和define的区别(php类中如何定义常量)
- C/C++中define定义的常量与const常量
- 常量定义 const 和define
- webwork的 <ww:radio 无法设置选择项显示默认项的问题
- JAVA开发之环境变量
- Spring 3.x jar 包详解 与 依赖关系
- java设计模式之Strategy
- Javascript中最常用的55个经典技巧
- php的常量定义:const VS define
- sed
- 编译boost.python库
- 关于Ognl一点警示
- Angel工作室精简版企业网站管理系统
- 关于memcpy
- gdb常用命令
- C语言变参的实现
- 自己实现RMI(六)压缩与解压