【问题标题】:Has any way to convert a name of constant or definition in a String? [php]有什么方法可以转换字符串中的常量或定义名称? [php]
【发布时间】:2014-06-18 11:51:05
【问题描述】:

我在 PHP 中有一个函数,我必须将字符串参数传递给该函数。我有一个包含许多常量定义的文件,例如:

define('MSG_CATEGORY_THINGS', things);

我想将我的常量MSG_CATEGORY_THINGS 转换为值'MSG_CATEGORY_THINGS' 的字符串,而不在参数中添加单引号。 有可能吗?

【问题讨论】:

标签: php constants


【解决方案1】:

您可以通过以下方式接收所有常量:

$constants = get_defined_constants(true);
$myConstants = $constants["user"];

这将返回一个关联数组,即

['MSG_CATEGORY_THINGS'=>things]

如果你想要它作为字符串,你可以这样做:

extract($myConstants)

您现在将拥有一个名为 $MSG_CATEGORY_THINGS 的变量,其值为 things :)

【讨论】:

    猜你喜欢
    • 2015-11-16
    • 2011-11-27
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2020-10-11
    相关资源
    最近更新 更多