相关疑难解决方法(0)

PHP中的动态常量名称

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

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;
Run Code Online (Sandbox Code Playgroud)

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

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

有人可以对此有所了解吗?

php constants indirection

72
推荐指数
3
解决办法
4万
查看次数

标签 统计

constants ×1

indirection ×1

php ×1