有人可以解释为什么一小段代码不起作用吗?
这是被赋予什么样的错误:
Parse error: syntax error, unexpected '=' in /var/www/g35003/
$img_attributes= style='max-height: 100px; max-width: 100px' . 'alt="'.$product['product_name'].'"';
有人可以解释为什么一小段代码不起作用吗?
这是被赋予什么样的错误:
Parse error: syntax error, unexpected '=' in /var/www/g35003/
$img_attributes= style='max-height: 100px; max-width: 100px' . 'alt="'.$product['product_name'].'"';
如果这是PHP和你想分配字符串到一个变量,应该有引号围绕字符串。
在这里,这个代码特定部分引起一个错误:
$img_attributes= style='max
应该有某种报价的第一=符号之后。
像这样的东西应该运行得更好,例如:
$img_attributes= 'style="max-height: 100px; max-width: 100px"'
. ' alt="' . $product['product_name'] . '"';
一点题外话:也许某种逃逸的可能是有帮助的,对于$product['product_name']
一部分?以确保它不包含任何会破坏您的标记的HTML。例如,
见htmlspecialchars
。
举例来说,如果您的产品名称被初始化是这样的:
$product['product_name'] = 'my mega "product"';
然后,用我前面贴的代码部分会得到你的输出:
style="max-height: 100px; max-width: 100px" alt="my mega "product""
这是不是不错...
使用htmlspecialchars
,像这样:
$img_attributes= 'style="max-height: 100px; max-width: 100px"'
. ' alt="' . htmlspecialchars($product['product_name']) . '"';
输出将成为:
style="max-height: 100px; max-width: 100px" alt="my mega "product""
其中,至少是在你的问题的有效调整,HTML的:-)
你搞乱了引号,可能的解决办法:
$img_attributes= "style='max-height: 100px; max-width: 100px'" . 'alt="'.$product['product_name'].'"';
一个部分我没有看到任何的JavaScript参考*或*所以我删除了这些标签并添加了PHP标签。标记是SO的一个重要方面,所以请尽量确保您选择与您的问题相关的标签,以便他们找到正确的专家。 – 2009-12-24 16:58:41