rich_traff Posted September 23, 2010 Share Posted September 23, 2010 Hi, im trying to apply a random css class to a div so that a random image is generated on each refresh. This is the code i am using to apply the class. <div class="<?php echo($randomimage); ?>"> Say i have an array of $image1 $image2 $image3 etc, how can i make $randomimage = 1 of these at random on each refresh? I've found lots of tutorials that generate a random number online but none that generate a random variable and am having trouble piecing this together. Any help would be hugely appreciated. Richard Link to comment https://forums.phpfreaks.com/topic/214188-how-can-i-generate-a-random-variable/ Share on other sites More sharing options...
Chris92 Posted September 23, 2010 Share Posted September 23, 2010 <div class="<?php echo $image[rand(1,3)]; ?>"> Link to comment https://forums.phpfreaks.com/topic/214188-how-can-i-generate-a-random-variable/#findComment-1114486 Share on other sites More sharing options...
rich_traff Posted September 23, 2010 Author Share Posted September 23, 2010 Thats great, thanks very much Link to comment https://forums.phpfreaks.com/topic/214188-how-can-i-generate-a-random-variable/#findComment-1114506 Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2010 Share Posted September 23, 2010 If the image filenames are in an array, you'd be better off using array_rand(). Then if the number of files changes, you don't have to edit the rand() function parameters. IOW, it would be more scalable. Link to comment https://forums.phpfreaks.com/topic/214188-how-can-i-generate-a-random-variable/#findComment-1114601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.