Random image with PHP

This little bit of php will give you a random image.

PHP
<?php
$links=array('wine.jpg','whisky.jpg','beer.jpg');
$rand=rand(0,2);
$image=$links[$rand];
echo '
<p><img src="'.$image.'" alt="'.substr($image,-4).'" /></p>';
?>

Here's the image...

beer

Refresh the page and you'll see that it's pretty random.

Here's one way to do it with javascript.

Comments

#1
2006-06-24 sushil kumar gupta says :

it is good

#2
2006-06-25 BonRouge says :

it is good
Wise words, sir!

#3
2007-01-01 twodayslate says :

Thank you so much!

#4
2009-06-04 xergos055 says :

The code is wrong. Is work with that:
<img src="'.$image.'" alt="'.substr($image, 0, -4).'" />

Comment form

Please type the word 'brown' here:

BB code available :

  • [b]...[/b] : bold
  • [it]...[/it] : italic
  • [q]...[/q] : quote
  • [c]...[/c] : code
  • [url=...]...[/url] : url