【发布时间】:2014-05-03 16:30:50
【问题描述】:
我希望 PHP 回显存储在变量中的字符串,然后紧跟一组包含其他文本的方括号。这进入了一个将被发送回 PHP 的表单,因此 PHP 需要能够将此命令的输出解释为一个数组项。
以下是我尝试过的方法:
$var='string';
echo "string[0]" //works, but only because the variable isn't used
echo "$var[0]"; //PHP tries to treat the string as an array
echo "$var\[0]"; //the slash gets echoed
echo "$var[0\]"; //syntax error
echo "$var"."[0]"; //this is what I'm using now. It's very ugly and I want an alternative
有什么方法可以在不将字符串分成块并将它们连接起来的情况下完成这项工作?
【问题讨论】:
-
把变量放在花括号里:
echo "{$var}[0]";