Have a random or new background image each page load

If you wondered how to do a new or random background image each time a web page is loaded here is how. Put the PHP script at the top of your web page and then the next segment of code inside your <head> section. You can have as many background images as you want, just state them in the array (note I was storing the images in the folder bg_images).

The PHP script:

$bg = array('bg1.jpg', 'bg2.jpg', 'b3.jpg', 'etcetc.jpg'); // array of background image filenames
$i = rand(0, count($bg)-1);
$selectedBg = "$bg[$i]";

The PHP inside HTML:

<style type="text/css">
body{
background: url(/bg_images/<?php echo $selectedBg; ?>) no-repeat;
}
</style>

Do this allows you to easily have a random or new background image each time your page is loaded.