Randomize Background Image


/ Published in: PHP
Save to your folder(s)

Set up and array of filenames, which correspond to the file names of the images you are trying to randomize.


Copy this code and paste it in your HTML
  1. <?php
  2. $bg = array('bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg', 'bg-07.jpg' ); // array of filenames
  3.  
  4. $i = rand(0, count($bg)-1); // generate random number size of the array
  5. $selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
  6. ?>
  7.  
  8. <style type="text/css">
  9. <!--
  10. body{
  11. background: url(images/<?php echo $selectedBg; ?>) no-repeat;
  12. }
  13. -->
  14. </style>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.