Reputation: 2752
I'm trying to make a header image change randomly based on a random number being chosen. 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
Upvotes: 2
Views: 5153
Reputation: 2752
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.
Upvotes: 1
Reputation: 5848
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.
Upvotes: 0
Reputation: 610
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)
Upvotes: 0
Reputation: 298196
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); ?>
Upvotes: 1