Use PHP to Get a Random Background Colour for a Website Content from the guide to life, the universe and everything

Use PHP to Get a Random Background Colour for a Website

4 Conversations

PHP (recursive acronym for 'PHP: Hypertext Preprocessor') is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
- The PHP Group

This quasi-tutorial will assume you already have some knowledge of PHP. Documentation and details of PHP can be found at The server hosting your site must also be able to handle PHP.

The Simple Way

Defining the Colours

Firstly, the colours to be chosen should be defined. It would be possible to create a script that would generate any HTML colour at random, but a set of colours that are complementary to the site is a better choice. The colours should be assigned to variables with an arbitrary letter and a number, for example:

$c1 = "#ffffff"; $c2 = "#ffff99"; $c3 = "#00ccff"; $c4 = "#66ff99"; $c5 = "#ffcc00"; $c6 = "#d7ebff"; $c7 = "#ccffcc";
The numbers should start at 1, and increase by 1 each time.

Choosing a Colour

This will be based on the rand() function which chooses a random number between, and including, two specified numbers. The minimum number will be 1, but the maximum number may not always be the same; colours may be added or removed.

The number of the last colour variable must be manually entered each time the number of colours changes, so the maximum would be set as 7 using the above colours, as shown:

$number = rand(1,7);
The first number is the minimum, and the second number is the maximum.

Setting the Background Colour

The colour variable corresponding to the number chosen by rand() needs to be assigned to a variable, for example:

$bgcolour = ${"c$number"};
This takes the value of $number, puts it after a c, and makes it a variable. This means that $bgcolour will have the value of one of $c1, $c2, $c3, etc depending on the chosen number.

The chosen colour must then be incorporated into the <BODY> tag. The easiest way to do this is to echo() the whole <BODY> tag at the end of the script, as shown:

echo ("<BODY BGCOLOR=\"$bgcolour\">");
The backslashes (\) before and after $bgcolour mean that the speech marks are output, rather than ending the printed text.


The finished script should look something like:

<?php $c1 = "#ffffff"; $c2 = "#ffff99"; $c3 = "#00ccff"; $c4 = "#66ff99"; $c5 = "#ffcc00"; $c6 = "#d7ebff"; $c7 = "#ccffcc"; $number = rand(1,7); $bgcolour = ${"c$number"}; echo ("<BODY BGCOLOR=\"$bgcolour\">"); ?>

The Advanced Way

The colours can be added to an array, the array counted, and this will be the maximum for rand().

Defining the Colours

The array of colours is defined using the array() construct, as below:

$colours = array( "#ffffff", "#ffff99", "#00ccff", "#66ff99", "#ffcc00", "#d7ebff", "#ccffcc" );

Choosing a Colour

The number of values in the array will be counted with count(), assigned to a variable, and set as the limit for rand(), as shown below:

$max = count($colours); $number = rand(1,$max);
With this method, you can add and remove colours, and the boundaries for rand() will be automatically adjusted.

Setting the Background Colour

The array element corresponding to the number chosen by rand() needs to be assigned to a variable, as shown:

$bgcolour = $colours[$number - 1];
It is necessary to subtract 1, as arrays are indexed from zero, rather than 1.

As in the simple way, the chosen colour must then be incorporated into the <BODY> tag, as below:

echo ("<BODY BGCOLOR=\"$bgcolour\">");


The finished script should look something like:

<?php $colours = array( "#ffffff", "#ffff99", "#00ccff", "#66ff99", "#ffcc00", "#d7ebff", "#ccffcc" ); $max = count($colours); $number = rand(1,$max); $bgcolour = $colours[$number - 1]; echo ("<BODY BGCOLOR=\"$bgcolour\">"); ?>

An Alternative Using PHP4

If your server supports PHP4, there is a new function called array_rand() that provides a random index into an array, replacing the need for count() and rand() altogether. A modified version of the advanced script, using array_rand(), is shown below:

<?php $colours = array( "#ffffff", "#ffff99", "#00ccff", "#66ff99", "#ffcc00", "#d7ebff", "#ccffcc" ); $bgcolour = $colours[array_rand($colours)]; echo ("<BODY BGCOLOR=\"$bgcolour\">"); ?>

Implementing the Script

Place the script of your choice after the </HEAD> closing tag, instead of the <BODY> opening tag, and make sure the filename ends with '.php'. Remember that not all servers support PHP, which is probably its biggest disadvantage.

The advantage of using PHP rather than JavaScript for this (and many things) is that it will work on any graphical browser, as it is the server - not the browser - that processes the PHP. Once the server has parsed the script, it outputs only HTML (or JavaScript, etc).

Bookmark on your Personal Space

Edited Entry


Infinite Improbability Drive

Infinite Improbability Drive

Read a random Edited Entry

Categorised In:

Written by


External Links

Not Panicking Ltd is not responsible for the content of external internet sites

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more