Dynamic constant name in PHP

后端 未结 3 863
不知归路
不知归路 2020-11-27 15:54

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 ...
$const         


        
3条回答
  •  青春惊慌失措
    2020-11-27 16:37

    And to demonstrate that this works with class constants too:

    class Joshua {
        const SAY_HELLO = "Hello, World";
    }
    
    $command = "HELLO";
    echo constant("Joshua::SAY_$command");
    

提交回复
热议问题