如何在稍后定义的字符串concat中使用变量?
问题描述:
我有一个空的数组中的变量。我给它一个价值并回应它,但它仍然是空的。如何在稍后定义的字符串concat中使用变量?
这里是我的代码:
$arr = array('0' => '<a href="www.example.com/vote.php?id='.$end['id'].'">');
while($end = mysql_fetch_assoc($result)){
$end['id']; // if I echo it, it will be a number, for e.g. 12
echo $arr[0]; // output: <a href="www.example.com/vote.php?id=">
}
为什么没有被替换的变量?如何在阵列中使用$end['id'];
?
我想这样的输出:
<a href="www.example.com/vote.php?id=12">
答
这里是解决方案:
$arr = array('0' => '<a href="www.example.com/vote.php?id=%d">');
...
echo sprintf($arr[0],$end['id']);
输出:
output: <a href="www.example.com/vote.php?id=12">
答
你可以简单地用这种方式
$arr = array('0' => '<a href="www.example.com/vote.php?id='".$end['id']."'">');
可能它会帮助
tldr;在* *赋值之前,不能在字符串concat中有用地使用变量。这个问题与数组无关,也可能以'$ result =' user2864740
你的数组右括号''''你可以通过代码中的高亮显示来看你的问题,代码有错误 – James
@James抱歉,只是一个错字 – Shafizadeh