Create Dynamic Cascading Style Sheets Using PHP

In the early days of the Internet, HTML grew to encompass a multitude of stylistic capabilities to give the designer more control on the appearance of the site. But this led to issues as different browsers had their own way of displaying the HTML code. The end result was inconsistency in the display of the website. Although style sheets have been in existence since the 1970’s, Cascading Style Sheets or CSS was developed to address the need for a consistent approach to provide style information for web documents.

Originally termed Personal Home Page, PHP was created in 1995 to replace certain PERL scripts. It was only in 1997 that it became the PHP: Hypertext Preprocessor that we know of today.

Using PHP to create dynamic CSS would be very useful if you think about it. You could create styles dynamically, reconfigure multiple facets of a style or several style-rules, use databases, server-side information and reduce coding.

If you want to try experimenting with this method, all you have to do is insert the following code (with php tags) to your CSS file:

header(“Content-type: text/css”);

Next, rename your stylesheet’s extension from the current “css” to “php”. Do similar changes on your webpage wherever you call the stylesheet. It is as simple as that. If you want to use variables, simply define the variables and then declare a php script with the variable. This also gives you the ability to access server-side data.