[web-tips > php style switcher]

PHP Style Switcher

Are you tired of looking for a javascript code that allows you to easily change style sheets? Do you want something that is simple, short, and will work if the user has turned off their javascript?

If so, this php technique is the slickest I have found.


Current style: style2.css

Style Sheet One

Style Sheet Two

The color of the above "Example" text was ruled "Red" inside cascading style sheet, but can be redefined by clicking Green or Red, which actually changes the style sheet used.

If your browser shows the color changes in above EXAMPLE text, then this technique works for your browser. As such, this technique will allow you to permit your user to set their own preferences.

The technique simply requires three things:

  • 1. Copy the following code and place it at the begining of your HTML document;
$thestyle = $_GET['set'];
if ($thestyle == "style1")
	$thestyle = "style2";
	$thestyle = "style1";

  • 2. In your HTML code between the <HEAD> tags, use the following stylesheet statement;

<link rel="stylesheet" type="text/css" media="screen" href="<?php echo($thestyle);?>.css" >
  • 3. Then in your HMTL document, place the following switch style links wherever you want.
<a href="index.php?set=style1">Style Sheet One</a>
<a href="index.php?set=style2">Style Sheet Two</a>

Make sure that your html document ends with the suffix of ".php". In other words, save the document as "index.php" and not "index.html".

Please donate $5 below for my development effort -- thank you.

This technique W3C validates under both HTML 4.01 Strict and CSS guidlines.