我有一个函数可以用来在表格中显示用户购物车中的商品:
function products() {
$get = mysql_query('SELECT id, name, description, price FROM menu WHERE quantity > 0');
if (mysql_num_rows($get) == 0) {
echo"Sorry, There is no products to display.";
}
else {
while ($get_row = mysql_fetch_assoc($get)) {
echo '<p>''<table>''<tr>''<td>'.$get_row['name'].'</td>''<td>'.$get_row['description'].'</td>''<td>$'.number_format($get_row['price'], 2).'</td>''<td>''<a href="cart.php?add='.$get_row['id'].'">Add To Cart</a></td></tr></table></p>';
}
}
}
不幸的是,当我尝试使用该代码时,出现以下错误:
Parse error: syntax error, unexpected
T_CONSTANT_ENCAPSED_STRING
, expecting,
or;
inC:\xampp\htdocs\shopping\cart.php
on line 37
37行是这一行:
echo '<p>''<table>''<tr>''<td>'.$get_row['name'].'</td>''<td>'.$get_row['description'].'</td>''<td>$'.number_format($get_row['price'], 2).'</td>''<td>''<a href="cart.php?add='.$get_row['id'].'">Add To Cart</a></td></tr></table></p>';
为什么会出现此错误,我该如何解决?
最佳答案
与某些其他语言不同,PHP不会自动连接连续的字符串文字。您必须显式串联:
echo 'a' . 'b' . 'c' . $someVar . 'd';
或使用逗号:
echo 'a', 'b', 'c', $someVar, 'd';
或者只是自己输入文字:
echo 'abc' . $someVar . 'd';
关于php - 为什么会收到有关T_CONSTANT_ENCAPSED_STRING的语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12189626/