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"; ?> Quote 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. Quote 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 Quote Link to comment https://forums.phpfreaks.com/topic/144384-random-image-not-working/#findComment-757641 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.