Jump to content

How can i generate a random variable?


rich_traff

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.