在PHP中有没有一种方法可以在字符串中包含常量而不进行连接?
define('MY_CONSTANT', 42);
echo "This is my constant: MY_CONSTANT";
发布于 2011-03-02 02:12:12
define( 'FOO', 'bar');
$FOO = FOO;
$string = "I am too lazy to concatenate $FOO in my string";
发布于 2010-02-08 22:56:09
define('FOO', 'bar');
$constants = create_function('$a', 'return $a;');
echo "Hello, my name is {$constants(FOO)}";
发布于 2010-02-05 07:36:14
你可以这样做:
define( 'FOO', 'bar' );
$constants = get_defined_constants(true); // the true argument categorizes the constants
$constants = $constants[ 'user' ]; // this gets only user-defined constants
echo "Hello, my name is {$constants['FOO']}";
https://stackoverflow.com/questions/2203943
复制相似问题