开发者

$img_attributes style question

Can somebody explain me why the small piece of code doesn't work?

This is the error wh开发者_运维技巧at is given:

Parse error: syntax error, unexpected '=' in /var/www/g35003/

$img_attributes= style='max-height: 100px; max-width: 100px' . 'alt="'.$product['product_name'].'"';


If this is PHP and you are trying to assign a string to a variable, there should be quotes arround the string.

Here, this specific portion of code is causing an error :

$img_attributes= style='max

There should be some kind of quote after the first = sign.


Something like this should work much better, for instance :

$img_attributes= 'style="max-height: 100px; max-width: 100px"'
     . ' alt="' . $product['product_name'] . '"';

As a sidenote : maybe some kind of escaping could be helpful, for the $product['product_name'] part ? to make sure it doesn't contain any HTML that would break your markup.
See htmlspecialchars, for instance.

For instance, if your product name is initialized this way :

$product['product_name'] = 'my mega "product"';

Then, using the portion of code I posted earlier will get you this output :

style="max-height: 100px; max-width: 100px" alt="my mega "product""

Which is not that nice...

Using htmlspecialchars, like this :

$img_attributes= 'style="max-height: 100px; max-width: 100px"'
     . ' alt="' . htmlspecialchars($product['product_name']) . '"';

The output would become :

style="max-height: 100px; max-width: 100px" alt="my mega "product""

Which, at least, is a portion of valid-HTML :-)


You are messing the quotes, possible fix:

$img_attributes= "style='max-height: 100px; max-width: 100px'" . 'alt="'.$product['product_name'].'"';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜