开发者

PHP print thumbnails simple problem

I'm trying to print some thumbnails using PHP. The problem is that I can't get the value of $thumbPath in the HTML. There is some little mistake that I can't see:

开发者_Python百科
for($i=1;$i<19;$i++){
                $thumbPath="img/theme".$i.".jpg";
                $thumbId="t".$i;
                echo '<li><a href="#">';
                echo '<img src=$thumbPath id=$thumbId border="none"/>';
                echo '</a></li>';
            }


Strings in single quotes do not parse variables. You either need to do this:

echo "<img src=$thumbPath id=$thumbId border=\"none\"/>";

or this:

echo '<img src='.$thumbPath.' id='.$thumbId.' border="none"/>';


Variables enclosed in single quotes don't evaluate to php assigned values.

Translated, using echo '$thumbPath' isn't the same as echo "$thumbPath".


change

echo '<img src=$thumbPath id=$thumbId border="none"/>';

to

echo '<img src="'.$thumbPath.'" id="'.$thumbId.'" border="none"/>';


echo '<img src='.$thumbPath.' id='.$thumbId.' border="none"/>';

Single quotes in php don't evaluate embedded variables. Hope it helps.


Personally I would do the following:

[echo s]printf('<img src="%s" id="%d" border="none" />', 
            $thumbPath, 
            $thumbId);

Obviously I wrapped the optional bits in the [] to show that you can echo sprintf() or just printf()

That way it looks nice and you can do exactly what you want with it. It's more secure too because the parameters which you are passing to the sprintf will be forced to be either a digit, string, float, etc.

Just an idea

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜