我们在对但是需要对PHP字符串中加入变量说明的是,尽管下面的例子没有错误:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">temp</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("one" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1, "two" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 2); </span> </li> <li class="alt"><span>// 输出:: The first element is 1 </span></li> <li class=""><span>echo "The first element is $temp[one]."; </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
但是如果后面那个 echo 语句没有双引号引起来的话,就要报错,因此建议使用花括号:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">temp</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("one" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1, "two" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 2); </span> </li> <li class="alt"><span>echo "The first element is {$temp["one"]}."; </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
以上就是PHP字符串中加入变量的具体解决方法。