backinblack Posted February 8, 2009 Share Posted February 8, 2009 I have a code that does a random image every time they come to the site but for some reason it is not working. I must be missing something. Can some one help me out? <?php $images = array('pic1', 'pic2', 'pic3'); $i = rand(0, count($images)-1); $selectedImage = "images/{$images[$i]}.jpg"; ?> Link to comment https://forums.phpfreaks.com/topic/144384-random-image-not-working/ Share on other sites More sharing options...
premiso Posted February 8, 2009 Share Posted February 8, 2009 Why not just use array_rand? <?php $images = array('pic1', 'pic2', 'pic3'); $image = $images[array_rand($images)]; $selectedImage = "images/{image}.jpg"; ?> EDIT: Fixed example. Link to comment https://forums.phpfreaks.com/topic/144384-random-image-not-working/#findComment-757637 Share on other sites More sharing options...
.josh Posted February 8, 2009 Share Posted February 8, 2009 The code itself is fine which means the problem is a) files not spelled right (case sensitive even) b) files do not exist c) wrong path/to/image Link to comment https://forums.phpfreaks.com/topic/144384-random-image-not-working/#findComment-757641 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.