PHP中的动态常量名称 [英] Dynamic constant name in PHP

查看:211
本文介绍了PHP中的动态常量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态创建一个常量名称,然后获取该值.

I am trying to create a constant name dynamically and then get at the value.

define( CONSTANT_1 , "Some value" ) ;

// try to use it dynamically ...
$constant_number = 1 ;
$constant_name = ("CONSTANT_" . $constant_number) ;

// try to assign the constant value to a variable...
$constant_value = $constant_name;

但是我发现$ constant值仍然包含常量的NAME,而不是VALUE.

But I find that $constant value still contains the NAME of the constant, and not the VALUE.

我也尝试了第二级间接寻址$$constant_name,但这会使它成为变量而不是常量.

I tried the second level of indirection as well $$constant_name But that would make it a variable not a constant.

有人可以对此有所启发吗?

Can somebody throw some light on this?

推荐答案

http: //dk.php.net/manual/zh/function.constant.php

echo constant($constant_name);

这篇关于PHP中的动态常量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆