Display Random Content in PHP

0
68

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.

  1. <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>

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.

Facebook Comments
(Visited 60 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here