语法
mixed constant ( string $name )
说明
constant() 函数用于返回一个常量的值。当预先不知道常量的名称,却需要取得该常量的值的时候,该函数特别有用。
通过向 $name 参数传递常量的名称,便可获得对应常量的值。
该函数对于类常量依然适用。
立即学习“PHP免费学习笔记(深入)”;
返回值
返回常量的值,若常量未定义,则返回 null,但此时会产生一个 E_WARNING 级别的错误。
示例
<?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!" 以上就是笔记018 PHP中的 constant() 函数的内容,更多相关内容请关注PHP中文网(www.php.cn)!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号