开发者

Generating a random number and adding to an HTML argument's string

I'm trying to make a header image change randomly based on a random number being ch开发者_StackOverflow中文版osen. This is the code I have right now, and it requires the entire tag.

<img src="http://www.example.com/site_gfx/headers/header_<?php echo(rand(1,7)); ?>.png" width="980" height="230" alt="Example Site" />

Is there any reason it would be dying like it is? Where the <?php echo part is is the PHP code i'm using to generate the random number, and I'd like to include that into the string for the img src


How's it dying? I'd try print or echo without parentheses, as I haven't seen echo() used before:

print():

<?php print(rand(1,7)); ?>

echo:

<?php echo rand(1,7); ?>


Figured it out. The problem is I didn't realize the <?php area was running within an echo command (stupid little oversight on my part). But modifying the echo statement that I was working with fixed it.

Thanks for the pointers, everyone.


Any reason you want to do this with PHP, it would probably be easier with javascript. try to remove the rand(1,7) from the bracket so as to have:

echo rand(1,7)


This would be a better approach to doing this

<?php
 $number = rand(1,7);
 echo '<img src="http://www.example.com/site_gfx/headers/header_' . $number . 'png" width="980" height="230" alt="Example Site" />'
?>

There may be an issue with using the <?php tag inside of another tag.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜