2

I just want to know if it's a good or bad idea to use a php css file performance wise. I really want to do a php method for the use of variables.

By php css file I mean:

<?php header("Content-type: text/css"); ?>

What are your thoughts on this?

daryl
  • 14,307
  • 21
  • 67
  • 92
  • 2
    I would opt for static CSS files -- even if they are dynamically generated. This allows for tools such as SASS/SCSS to be used as well as it plays nicer with "editors that understand CSS" and avoids "too much clever nonsense" being used. (I would argue there are normally better approaches to this problem.) –  May 25 '11 at 01:11

3 Answers3

6

Nope, it's fairly common in the wild. The only performance hit is when you output the file dynamically, which can be mitigated easily with server-side (and client side) caching.

Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
2

This is a bad idea, but only if you don't cache.

On the server side, I expect that you're using some sort of global site-level variables, then building your CSS from that. Fine, just make sure that you save the file, invalidating it whenever it needs to change (say, from an SVN checkin).

From the client's perspective, you want to ensure that it doesn't have to download it on every page load. For that you'll need to properly set last-modified headers or an Etag.

John Green
  • 13,241
  • 3
  • 29
  • 51
  • I see, any tips on how to go about setting a cache header? – daryl May 25 '11 at 01:11
  • Heh. That's a longer answer. : ) Can you check the filemtime() of your CSS, or do you have a chain of dependent classes up the way? If the former, you can use the last-modified, if the latter, you're best off using an Etag. Pick a route and I'll drop a solution. Here's one for last-mod: http://stackoverflow.com/questions/2451901/304-not-modified-issue – John Green May 25 '11 at 01:14
0

I don't think it's a bad idea, and I'm currently doing it in that way. In my framework I dynamically concatenate two css groups in two separate files, the site main CSSs (layout, forms, etc) and some module specific ones if they exists. So, they can be cached separately. I also include some dynamic path inside using php vars.

morgar
  • 2,339
  • 17
  • 16