2

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

4
  • are the photo's extention .png or .PNG? Could make a difference if case sensitive. Commented May 12, 2011 at 23:54
  • Is this a PHP page? Does it end with .php? Commented May 12, 2011 at 23:59
  • they're lowercase extensions, I checked. And this is a .php page its on. Commented May 13, 2011 at 0:01
  • The PHP isn't generating the number, as such the image that would have the number is 404-ing and alt-text is showing up. Commented May 13, 2011 at 0:14

4 Answers 4

1

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); ?>
Sign up to request clarification or add additional context in comments.

2 Comments

no difference for print and echo to just spit out a simple text of something. At least not in my experiences.
print is also not a function, but a struct, so print X; without parenthesis is 'better'.
1

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.

Comments

0

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)

2 Comments

How so? PHP is the most cross-browser, as it's browser-independent.
JavaScript isn't an option, I'm trying to avoid non-PHP scripts.
0

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.

3 Comments

Don't need (shouldn't actually) to escape double-quotes inside single quotes. That will render as exactly \".
@EvilPheonix He has a opening PHP tag <?php in his answer, but did not escape it, so S.O. strips it (and everything after it) from the content of his answer.
@Tieson T. Good point, my mistake, the escape characters for the double quotes should be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.