当前位置:Gxlcms > PHP教程 > 笔记018PHP中的constant()函数

笔记018PHP中的constant()函数

时间:2021-07-01 10:21:17 帮助过:25人阅读

语法

mixed constant ( string $name )

说明

constant() 函数用于返回一个常量的值。当预先不知道常量的名称,却需要取得该常量的值的时候,该函数特别有用。

通过向 $name 参数传递常量的名称,便可获得对应常量的值。

该函数对于类常量依然适用。

返回值

返回常量的值,若常量未定义,则返回 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.gxlcms.com)!

人气教程排行