Display Random Content in PHP


Hello! Good day everyone. Today, I’m gonna teach you on Displaying a Content Randomly in PHP. Let’s start our tutorial.

To be able to make it possible, we will use the rand() function to generate a random number. This function generate numbers with-in a given range. The random number generator should be seeded to prevent a regular pattern of numbers being generated.

To be able to make it, we will be using srand() function that specifies the seed number as its argument.

The codes below demonstrates on displaying different image in every timeout.

[html]<html><html> <body> <?php srand(microtime() * 100000); $num = rand(1, 4); $switch($num) { case 1: $image = “images/img1.jpg”; break; case 2: $image = “images/img2.jpg”; break; case 3: $image = “images/img3.jpg”; break; case 4: $image = “images/img4.jpg”; break; } echo “<img src=”$image” />”; ?> </body></html>[/html]

Thats all.

If you have questions regarding this tutorial entitled as “Display Random Content in PHP” feel free to ask us by commenting below or visit on our contact page. Thank you.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.