首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在字符串中包含常量而不进行连接

在字符串中包含常量而不进行连接
EN

Stack Overflow用户
提问于 2010-02-05 07:13:05
回答 8查看 78.1K关注 0票数 186

在PHP中有没有一种方法可以在字符串中包含常量而不进行连接?

define('MY_CONSTANT', 42);

echo "This is my constant: MY_CONSTANT";
EN

回答 8

Stack Overflow用户

发布于 2011-03-02 02:12:12

define( 'FOO', 'bar');  
$FOO = FOO;  
$string = "I am too lazy to concatenate $FOO in my string";
票数 24
EN

Stack Overflow用户

发布于 2010-02-08 22:56:09

define('FOO', 'bar');
$constants = create_function('$a', 'return $a;');
echo "Hello, my name is {$constants(FOO)}";
票数 14
EN

Stack Overflow用户

发布于 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']}";
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2203943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档