php如何查看常量是否定义,PHP 检查是否定义了常量

示例

简单检查

要检查是否定义了常数,请使用defined函数。请注意,此函数并不关心常量的值,它仅关心常量是否存在。即使常量的值为null或false函数仍将返回true。

define("GOOD", false);

if (defined("GOOD")) {

print "GOOD is defined" ; // prints "GOOD is defined"

if (GOOD) {

print "GOOD is true" ; // 不打印任何内容,因为GOOD为假

}

}

if (!defined("AWESOME")) {

define("AWESOME", true); //真棒没有定义。现在我们已经定义了

}

请注意,只有在定义常量的行之后,常量才会在代码中“可见” :

if (defined("GOOD")) {

print "GOOD is defined"; // 不打印任何内容,尚未定义好。

}

define("GOOD", false);

if (defined("GOOD")) {

print "GOOD is defined"; // prints "GOOD is defined"

}

获取所有定义的常量

要获取所有定义的常量,包括由PHP创建的常量,请使用get_defined_constants函数:

$constants = get_defined_constants();

var_dump($constants); // 很大的清单

要仅获取由您的应用定义的常量,请在脚本的开头和结尾处调用函数(通常在引导过程之后):

$constants = get_defined_constants();

define("HELLO", "hello");

define("WORLD", "world");

$new_constants = get_defined_constants();

$myconstants = array_diff_assoc($new_constants, $constants);

var_export($myconstants);

/*

Output:

array (

'HELLO' => 'hello',

'WORLD' => 'world',

)

*/

有时对调试很有用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值