如何将字符串和变量连接成常量名称?
我有一个像define("EMPLOYEE_NAME_ID","employee");这样的常量和一个变量 $code = EMPLOYEE;
现在我想像下面一样打印
<?php echo $code.NAME_ID; ?>
但这只打印“EMPLOYEE_NAME_ID”而我想打印“employee”。那么如何打印这个呢。全部意思是我想从 lang 文件中检索变量。
I have a constant like define("EMPLOYEE_NAME_ID","employee"); and a variable $code = EMPLOYEE;
now i want to print like below
<?php echo $code.NAME_ID; ?>
But this prints only "EMPLOYEE_NAME_ID" and i want to print "employee". Then how to print this. The all over means is that i want to retriew variables from lang file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
PHP 中不带引号的字符串将被解析为常量,
如果常量未定义,
它将被视为字符串(而不是变量)
如果您处理常量,您可以使用 常量函数 :-
但是,如果未定义常量,使用此函数将返回警告消息。
还有其他选项,例如 parse_ini_file 你可以看看,< br>
这是处理大量设置/配置的理想选择
A unquote string in PHP will be parsed as constant,
and if the constant is undefined,
it will treat as the string (instead of a variable)
If you dealing with constant, you can make use of constant function :-
However, use of this function will return warning message if the constant is not defined.
There are other option like parse_ini_file you can take a look,
this is ideal for handling large amount of setting / configuration
更好的方法是使用 constant 函数
Better way would be to use constant function
两者之一是常量,而不是变量。您试图像变量变量(通常是<在这种情况下,应该首选 href="http://php.net/array" rel="nofollow">
array
)。但是,您可以使用
constant()
查找函数,通过常量实现相同的效果:请注意,您仍然需要额外的
_
下划线才能正常工作,而您的常量后缀和$code
都不包含该下划线。One of the two is a constant, not a variable. You were attempting to use them like variable variables (oftentimes an
array
should be preferred in such circumstances).You can however achieve the same effect with constants, using the
constant()
lookup function:Note that you still need the extra
_
underscore for this to work, which neither your constant suffix nor$code
contained.define("EMPLOYEE_NAME_ID","employee")
是一个常量,因此将其与变量组合时,您的语法应如下所示。define("EMPLOYEE_NAME_ID","employee")
is a constant so when combining it with a variable your syntax should be like this.