导航首页 » 技术教程 » PHP:constant()的用法_misc函数
PHP:constant()的用法_misc函数 172 2023-12-15   

constant

(PHP 4 >= 4.0.4, PHP 5)

constant — 返回一个常量的值

说明

mixed constant ( string $name )

通过 name 返回常量的值。

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

该函数也适用 class constants。

参数

name

常量名。

返回值

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

错误/异常

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

范例

Example #1 constant() 的例子

<?php

define("MAXSIZE", 100);

echo MAXSIZE;
echo constant("MAXSIZE"); // same thing as the previous line


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() - 检查某个名称的常量是否存在 Constants 这一节


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103