defined() - php 选项信息函数
defined()
(PHP 4, PHP 5, PHP 7)
检查某个名称的常量是否存在
说明
defined(string $name): bool检查该名称的常量是否已定义。
Note:如果你要检查一个变量是否存在,请使用isset()。defined()函数仅对constants有效。如果你要检测某个函数是否存在,使用function_exists()。
参数
$name常量的名称。
返回值
如果名称$name的常量已定义,返回TRUE
;未定义则返回FALSE
。
范例
检查常量
参见
define()
定义一个常量constant()
返回一个常量的值get_defined_constants()
返回所有常量的关联数组,键是常量名,值是常量值function_exists()
如果给定的函数已经被定义就返回 TRUE- 章节Constants
My preferred way of checking if a constant is set, and if it isn't - setting it (could be used to set defaults in a file, where the user has already had the opportunity to set their own values in another.) Dan.
You can use the late static command "static::" withing defined as well. This example outputs - as expected - "int (2)"
Dont forget to put the name of your constant into single quotation mark. You will not get an error or a warning.
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)