如何将字符串和变量连接成常量名称?

发布于 2024-12-18 13:52:51 字数 239 浏览 1 评论 0原文

我有一个像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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小兔几 2024-12-25 13:52:51

PHP 中不带引号的字符串将被解析为常量,
如果常量未定义,
它将被视为字符串(而不是变量)

如果您处理常量,您可以使用 常量函数 :-

echo constant("{$code}_NAME_ID");

但是,如果未定义常量,使用此函数将返回警告消息。
还有其他选项,例如 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 :-

echo constant("{$code}_NAME_ID");

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

橪书 2024-12-25 13:52:51

更好的方法是使用 constant 函数

echo constant($code."NAME_ID");

Better way would be to use constant function

echo constant($code."NAME_ID");
庆幸我还是我 2024-12-25 13:52:51

两者之一是常量,而不是变量。您试图像变量变量(通常是<在这种情况下,应该首选 href="http://php.net/array" rel="nofollow">array)。

但是,您可以使用 constant() 查找函数,通过常量实现相同的效果:

 <?php echo constant("{$code}_NAME_ID"); ?>

请注意,您仍然需要额外的 _ 下划线才能正常工作,而您的常量后缀和 $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:

 <?php echo constant("{$code}_NAME_ID"); ?>

Note that you still need the extra _ underscore for this to work, which neither your constant suffix nor $code contained.

み格子的夏天 2024-12-25 13:52:51
   define('VAR1', 'my var' );

   $var2 = 'hello';

   echo $var2.''.VAR1;  // period, two apostrophes, period

   // same as if you were doing:

   echo "some string ".$var2." some more string".VAR1;
   define('VAR1', 'my var' );

   $var2 = 'hello';

   echo $var2.''.VAR1;  // period, two apostrophes, period

   // same as if you were doing:

   echo "some string ".$var2." some more string".VAR1;
花间憩 2024-12-25 13:52:51

define("EMPLOYEE_NAME_ID","employee") 是一个常量,因此将其与变量组合时,您的语法应如下所示。

$temp = "John";

$var = EMPLOYEE_NAME_ID." : ".$temp;

echo $var; {Output : employee : john}

define("EMPLOYEE_NAME_ID","employee") is a constant so when combining it with a variable your syntax should be like this.

$temp = "John";

$var = EMPLOYEE_NAME_ID." : ".$temp;

echo $var; {Output : employee : john}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文