Display Random Content in PHP

Hello! Good day everyone. Today, I’m gonna teach you on Displaying a Content Randomly in PHPLet’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.

1 thought on “Display Random Content in PHP”

  1. sir, I want to random desplay(echo), my database’s id number with name or other cul.
    I want user click button and desplay any number (which are random)
    and set time (expairy time) then stop last number with other data.

    how to possible

Leave a Comment