I had always wished CSS files weren’t so static so you could do different things at runtime. It turns out you can do it, but exercise common sense. The exact plan you take depends on how often the CSS “variables” change value. If it’s not too often you might want to “precompile” the style files into static and just use them the old style way.
Here is how you can use PHP to do ‘on-the-fly’ CSS files.
- Instead of including a static css file include a file with the php suffix as below:
<link rel='stylesheet' type='text/css' href='css/style.php' />
- Make sure your style.php file returns the appropriate headers for a text/css mime type as below:
header("Content-type: text/css; charset: UTF-8"); - Now set up variables in your style.php very much like you would for a view template:
$mainUrl = "http://dorkage.net"; $mainHeaderColor = "#FE0EDE";
- Refer to your variables using echo
#header { background: url("<?php echo $mainUrl; ?>/images/header-bg.png") no-repeat; } a { color: <?php echo $mainHeaderColor; ?>; }
CSS Tricks.com reported a different way to do this: This match rule in htaccess matches any file with style in it’s name and gets parsed as PHP thusly:
<FilesMatch "^.*?style.*?$"> SetHandler php5-script </FilesMatch>
They speculate that you wouldn’t be able to take advantage of the automatic cacheing that is normally provided for style files. However, you could precompile your style.php by running it through CLI php each time you update the “variables” thusly:
php style.php > style.css
and just include the resulting css file the normal way








Here is my latest geeky plarn project. It’s been a long time in the making. I’m very proud of how long it took me to get enough bags to do this project, and I have to say I didn’t even get all of them myself. I fashioned the bag as a backpack rather than a shopping bag so I could operate it hands-free. I got the straps too long, so I just tucked up the extra strappage. I can always undo that if a larger person ever uses the backpack. My try at doing the PHP elephant was a limited by the resolution of this large needle knitting. Also he had to be vertical, and he had to be across the short way so that he would have the right orientation and so that he wouldn’t be covered up when the top is closed. I have the pattern I created for the elephant if anyone really wants it. You could probably improve it with a slightly finer resolution–I only had 30 st of width to work with. 