大括号中的变量 - PHP 编码问题
这是我的 PHP/MySQL 脚本:
<?php
mysql_connect('localhost', 'root', 'test') or die (mysql_error());
mysql_select_db('info1') or die (mysql_error());
$result = mysql_query("SELECT * from automobiles");
//Table starting tag and header cells
while($row = mysql_fetch_array($result)){
//Display the results in different cells
echo "<dd><dl><img src=' " . $row['image'] . " '>" . $row['manufacturer'] ." " . $row['model'] . "</dd></dl>";
echo "<dd><dl>" . $row['carinfo'] . "</dd></dl>";
}
//Table closing tag
echo "</table>";
?>
但是,如果我这样做,它会工作吗:
{$myvariable}
用大括号对变量进行编码是个好主意吗?或者:
echo " . $row['variable']. "
感谢任何帮助,谢谢!
This is my PHP/MySQL script:
<?php
mysql_connect('localhost', 'root', 'test') or die (mysql_error());
mysql_select_db('info1') or die (mysql_error());
$result = mysql_query("SELECT * from automobiles");
//Table starting tag and header cells
while($row = mysql_fetch_array($result)){
//Display the results in different cells
echo "<dd><dl><img src=' " . $row['image'] . " '>" . $row['manufacturer'] ." " . $row['model'] . "</dd></dl>";
echo "<dd><dl>" . $row['carinfo'] . "</dd></dl>";
}
//Table closing tag
echo "</table>";
?>
However, would it work if I did it this way:
{$myvariable}
Is it a good idea to code the variables with the braces, or as:
echo " . $row['variable']. "
Any help is appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样做:
你不能这样做:
这也行不通:
你的输出实际上是:
这是由于使用单引号而不是双引号造成的。错误是因为您没有转义变量内的单引号。
如果您这样做:
您的输出实际上将是:
对于相同的单引号与双引号推理。
You can do this:
You cannot do this:
This also wont work:
Your output would actually be:
This is due to using single quotes instead of double quotes. And the error would be because you did not escape the single quotes inside the variable.
If you did this:
Your output would actually be:
For the same single quote vs double quote reasoning.
我个人更喜欢使用
" " 。 $行['变量']。 “”
语法,因为它更容易阅读代码,特别是如果您有代码语法荧光笔。但使用这种语法是可以接受的:"{$var['field']}"
。I personally prefer using the
" " . $row['variable']. " "
syntax because it's easier to read code, specially if you have a code syntax highlighter. But using this syntax is acceptable:"{$var['field']}"
.