constant 返回一个常量的值

发表日期:2021-07-01 10:15:42 | 来源: | | 浏览(815) 分类:杂项 函数

constant

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

constant返回一个常量的值

说明

constant(string $name): mixed

返回 name 对应的常量的值。

当你不知道常量名,却需要获取常量的值时,constant() 就很有用了。也就是说,常量名储存在一个变量里,或者由函数返回时。

该函数也适用 类常量

参数

name

常量名。

返回值

返回常量的值。如果常量未定义则返回 null

错误/异常

如果常量未定义,会产生一个 E_WARNING 级别的错误。

范例

示例 #1 constant() 的例子

<?php 
define("MAXSIZE", 100);
echo MAXSIZE;
echo constant("MAXSIZE");
 // 和上行一样interface bar {
    const test = 'foobar!';
}
class foo {
    const test = 'foobar!';
}
$const = 'test';
var_dump(constant('bar::'. $const));
 // string(7) "foobar!"var_dump(constant('foo::'. $const));
 // string(7) "foobar!"?>

参见

  • define() - 定义一个常量
  • defined() - 检查某个名称的常量是否存在
  • 关于 常量 的章节

集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。