define() - php 选项信息函数
define()
(PHP 4, PHP 5, PHP 7)
定义一个常量
说明
define(string $name,mixed $value[,bool $case_insensitive= false]): bool在运行时定义一个常量。
参数
$name常量名。
$value常量的值;在 PHP 5 中,$value必须是标量(integer、float、string、boolean、NULL
)在 PHP 7 中还允许是个array的值。
常量还可以定义为resource类型,但并不推荐这样做,因为可能会有不可预知的行为发生。
$case_insensitive如果设置为TRUE
,该常量则大小写不敏感。默认是大小写敏感的。比如,CONSTANT和Constant代表了不同的值。
大小写不敏感的常量以小写的方式储存。
返回值
成功时返回TRUE
,或者在失败时返回FALSE
。
更新日志
版本 | 说明 |
---|---|
7.0.0 | 允许array的值。 |
范例
定义常量
参见
defined()
检查某个名称的常量是否存在constant()
返回一个常量的值- Constants这一节
Be aware that if "Notice"-level error reporting is turned off, then trying to use a constant as a variable will result in it being interpreted as a string, if it has not been defined. I was working on a program which included a config file which contained: Since I wanted to remove the ability for uploads, I changed the file to read: However, to my surprise, the program was still allowing uploads. Digging deeper into the code, I discovered this: Since 'ENABLE_UPLOADS' was not defined as a constant, PHP was interpreting its use as a string constant, which of course evaluates as True.
You can define constants with variable names (works also with constant values or variables or array values or class properties and so on - as long it's a valid constant name). Needless to say that you'll lose your IDE support for refactoring and highlighting completely for such cases. No clue why someone would / could actually use this but i thought it's worth mentioning.
define() will define constants exactly as specified. So, if you want to define a constant in a namespace, you will need to specify the namespace in your call to define(), even if you're calling define() from within a namespace. The following examples will make it clear. The following code will define the constant "MESSAGE" in the global namespace (i.e. "\MESSAGE"). The following code will define two constants in the "test" namespace.
Not sure why the docs omit this, but when attempting to define() a constant that has already been defined, it will fail, trigger an E_NOTICE and the constant's value will remain as it was originally defined (with the new value ignored). (Guess that's why they're called "constants".)
The value of a constant can be the value of another constant.
This is obvious, but easy to forget: if you include a file, the include file can only make use of constants already defined. For example:
There's an undocumented side-effect of setting the third parameter to true (case-insensitive constants): these constants can actually be "redefined" as case-sensitive, unless it's all lowercase (which you shouldn't define anyway). The fact is that case-sensitive constants are stored as is, while case-insensitive constants are stored in lowercase, internally. You're still allowed to define other constants with the same name but capitalized differently (except for all lowercase). Why would you use this? A third party plugin might attempt to define a constant for which you already set a value. If it's fine for them to set the new value, assuming you cannot edit the plugin, you could define your constant case-insensitive. You can still access the original value, if needed, by using any capitalization other than the one the plugin uses. As a matter of fact, I can't think of another case where you would want a case-insensitive constant...
Found something interesting. The following define: Will return a '0'. Whereas this: Will return 'This is a test'. This may be common knowledge but I only found out a few minutes ago. [EDIT BY danbrown AT php DOT net: The original poster is referring to the hyphens versus underscores. Hyphens do not work in defines or variables, which is expected behavior.]
Php 7 - Define: "Defines a named constant at runtime. In PHP 7, array values are also accepted." But prior PHP 7, you can maybe do this, to pass an array elsewhere using define: $to_define_array = serialize($array); define( "DEFINEANARRAY", $to_define_array ); ... and so ... $serialized = DEFINEANARRAY; // passing directly the defined will not work $our_array = unserialize($serialized); print_r($our_array);
I think worth mentioning is that define() appears to ignore invalid constant names. One immediate implication of this seem to be that if you use an invalid constant name you have to use constant() to access it and obviously that you can't use the return value from define() to tell you whether the constant name used is invalid or not. For example: $name = '7(/!§%'; var_dump(define($name, "hello")); // outputs bool(true) var_dump(constant($name)); // outputs string(5) "hello"
It may be worth stating that a define function must be executed before its global constant is referenced. Example: Abc(); define("TEST", 23); function Abc() { echo TEST; } // Abc This code fails with a Notice-level message. TEST is treated here as being the string "TEST".
For translating with variables and define, take also a look on the constant() function. Example :
To clear up a few thing: Integers with 0 in front work. But since PHP (and many other languages) handle them as octal values, they're only allowed a range of 0-7: Result: 448 0 --------------------------------------- Furthermore, writing the constant name without the quotation-marks (as mentioned in the notes) throws an E_NOTICE and should be avoided! Result: Notice: Use of undefined constant TEST - assumed 'TEST'
With php 7 you can now define arrays. consider the following code:
You can also define associative array as constant:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)