【问题标题】:Use variable contents inside variable name with php? [duplicate]在 php 中使用变量名中的变量内容? [复制]
【发布时间】:2012-10-09 18:04:03
【问题描述】:

可能重复:
Appending a value of a variable to a variable name?

我完全搞不懂语法,搜索的范围很广。

我想这样做:

$uni = "ntu";
$selectedntu = "something";

echo $selected$uni;
// output should be the same as
echo $selectedntu;

换句话说,我想使用第二个变量 $uni 的内容来加入第一个变量的名称。 $selectedntu 已经设置了一个 foreach 循环,但我不知道如何在 php 中同时引用这两个变量。

【问题讨论】:

标签: php variables syntax


【解决方案1】:

构造字符串并使用变量变量$$

$uni = "ntu";
$selected = "something";

$new_variable = $selected . $uni; 
echo $$new_variable;

// 或者..根据你的googleing..纯粹供以后参考

$uni = "ntu";
$selected = "something";

echo ${$selected . $uni};  

【讨论】:

  • ${"selected".$uni} 是您所追求的。否则你必须先定义$selected="selected";
  • 是的,干杯,我不想/不需要定义另一个变量,所以 echo ${$var.$var2};完美运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 2018-05-22
  • 2014-06-06
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
相关资源
最近更新 更多