duanji9677 2017-01-17 12:07
浏览 36
已采纳

是否可以组成常量名称?

Is there any way to call/compose a constant name in PHP, which does not require the use of eval?

I mean, I have several constant names following this schema:

define("CONSTANT_0", "foo 0");
define("CONSTANT_1", "foo 1");
define("CONSTANT_2", "foo 2");

and I want to be able to go through those values in a loop, something like this:

for ($i=0; $i<3; $i++)
   echo CONSTANT_$i;

I can't use variables here because I'm using a pre-defined shared class, which already contains those values.

Thanks in advance

  • 写回答

3条回答 默认 最新

  • dougaodi8895 2017-01-17 12:12
    关注

    This would help -

    define("CONSTANT_0", "foo 0");
    define("CONSTANT_1", "foo 1");
    define("CONSTANT_2", "foo 2");
    
    for ($i=0; $i<3; $i++) {
          echo constant('CONSTANT_' . $i);
    }
    

    Output

    foo 0foo 1foo 2
    

    constant()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试