Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why simply use PHP variables for dynamic stylesheets?

Tags:

css

php

sass

less

I've been reading about dynamic stylesheets and have stumbled across several options, including sass, and less. But my question is why not just turn my stylesheet.css into stylesheet.css.php and simply use php variables. Then, I avoid all the dependency issues associated with all these other approaches.

Am I overlooking some serious problems by doing it this way?

like image 409
Jeff Avatar asked Jan 13 '23 14:01

Jeff


2 Answers

There is the argument of code re-use: when writing PHP code to generate CSS, you're effectively duplicating (some) of the logic behind things such as sass and less. Why would you do that when there's a widely-used, tested and complete alternative available?

Another thing is performance. Standard CSS files are served by your web server with sane headers regarding caching by the browser. Your browser will not download that same CSS file each time, it just gets it from the browser-side buffer. By default, PHP is not cached at all (and you usually wouldn't want it to be). This means that, by default, your PHP-generated CSS would not be cached, incurring extra load on your server and extra waiting time for your client. While some of this can be solved (including sane header output in the PHP code that generates your CSS), some of it cannot (the overhead of the web server starting up PHP, for example).

like image 57
Tomas Creemers Avatar answered Jan 20 '23 05:01

Tomas Creemers


Am I overlooking some serious problems by doing it this way?

I host all static assets on a CDN, which you should too. CDNs don't do PHP.

Also: caching, runtime performance, minification

like image 42
Prinzhorn Avatar answered Jan 20 '23 05:01

Prinzhorn