The original content of this site. for reprinted content, please indicate the source webpage. We once visited such a website and made random changes every time we refreshed the banner. in this article, we will show you how to use PHP to achieve this function. Principle of STEP program implementation
Original content of this site. for reprinted content, please indicate the sourceWebpage Tutorial network.
We once visited such a website and made random changes every time we refreshed the banner. in this article, we will show you how to use PHP to achieve this function.
Procedure
Program Implementation principle: call an array, each image corresponds to an element in an array, then we set a random number, as long as a random data can be displayed a pair of images.
The first step is to generate a random number. Each time we refresh, we get different random numbers. the specific code is:
Srand (float) microtime () * 10000000 );
Then we set an array to image, and then set five array elements. the code is as follows:
$ Image [1] = '/location/of/image1.jpg ';
$ Image [2] = '/location/of/image2.jpg ';
$ Image [3] = '/location/of/image3.jpg ';
$ Image [4] = '/location/of/image4.jpg ';
$ Image [5] = '/location/of/image5.jpg ';
The following code randomly selects an element from the array:
$ Rn = array_rand ($ image );
Then we will display a random image:
Echo '';
Combine the above code.
Srand (float) microtime () * 10000000 );
$ Image [1] = '/location/of/image1.jpg ';
$ Image [2] = '/location/of/image2.jpg ';
$ Image [3] = '/location/of/image3.jpg ';
$ Image [4] = '/location/of/image4.jpg ';
$ Image [5] = '/location/of/image5.jpg ';
$ Rn = array_rand ($ image );
Echo '';
The above code is the code for randomly displaying images. if we want to add a connection address to each image, we can slightly correct the above code! Change the preceding array to a two-dimensional array:
$ Image [1] ['Pic '] ='/location/of/image1.jpg ';
$ Image [1] ['link'] = '/location/of/link1.php ';
The corresponding display code is:
Echo '';
Echo '';
Then we can complete our title function, randomly display images and connect them to different specified addresses:
Srand (float) microtime () * 10000000 );
$ Image [1] ['Pic '] ='/location/of/image1.jpg ';
$ Image [1] ['link'] = '/location/of/link1.php ';
$ Image [2] ['Pic '] ='/location/of/image2.jpg ';
$ Image [2] ['link'] = '/location/of/link2.php ';
$ Image [3] ['Pic '] ='/location/of/image3.jpg ';
$ Image [3] ['link'] = '/location/of/link3.php ';
$ Image [4] ['Pic '] ='/location/of/image4.jpg ';
$ Image [4] ['link'] = '/location/of/link4.php ';
$ Image [5] ['Pic '] ='/location/of/image5.jpg ';
$ Image [5] ['link'] = '/location/of/link5.php ';