0

I'm using multiple CSS file in a project and also using a Php file as "stylesheet" to load all the CSS into a single file with compress them all. For that I'm using the following code:

$cssFiles = array("reset.css", "main.css", "plugin.css");

$css = "";

foreach($cssFiles as $cssFile){
    $css .= file_get_contents($cssFile);
}

header("Content-type: text/css");

echo($css);

Now I need to know what is the advantages or disadvantages of this process?

I'm doing the same thing for javascript files too.

Thanks.

M B Parvez
  • 808
  • 6
  • 16
  • 1
    What if you use grunt to combine them and minify them?. – botero Feb 08 '15 at 23:16
  • 3
    The principle of server-side generated or composed CSS and scripts is fine, but loading all of the text into a PHP string `$css` is inefficient, just write it directly to the output stream. – Dai Feb 08 '15 at 23:17
  • yeah @botero is right, your process really don´t have any issues more than inefficience in loading, but professional tools like grunt do this tasks for you. – Edgar Orozco Feb 08 '15 at 23:19
  • I think this isnt stupid, only lacks of experience and knowledge. By the way this is the right way to tackle this gap, ask in SO for kindly help of the community! – Edgar Orozco Feb 08 '15 at 23:25
  • You can set up PHP to cache the response as well. Reducing the number of HTTP calls is likely worth the overhead – Tom B Feb 08 '15 at 23:25
  • Ya, Grant is here to do the same thing with more professional look. I'm using PHP to minifing my CSS and also using cache the response as well. But that code(s) are not in the snippets because my question was not on "My process vs Grant" or "Minify my CSS" or not even on "caching". My question is "pros. or cons. of CSS or Javascript into a PHP file?". And the only thing I got from the above questions is the process is not too professional. Is there anything else? I'm asking @OmeCoat. Thanks to you all. – M B Parvez Feb 10 '15 at 17:40

2 Answers2

1

I would suggest instead of doing that using php, use grunt to combine them and minify them

This question explains better how you can do that:

How to concatenate and minify multiple CSS and JavaScript files with Grunt.js (0.3.x)

Community
  • 1
  • 1
botero
  • 598
  • 2
  • 11
  • 23
1

Cons:

  • The processor will load the charge of the parsing, interpreting and finally output the CSS and JS process
  • Server Memory will load the same charge for this processes.

Pros: I Really found hard to think in any pro here... maybe the non use of other tools than PHP to implement dynamic CSS and JS?

Edgar Orozco
  • 2,722
  • 29
  • 33