Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving php as css/js: Is it fast enough? What drawbacks are there?

I've recently started getting into the area of optimizing preformance and load times client side, compressing css/js, gzipping, paying attention to YSlow, etc.

I'm wondering, while trying to achieve all these micro-optimizations, what are the pros and cons of serving php files as css or javascript?

I'm not entirely sure where the bottleneck is, if there is one. I would assume that between an identical css and php file, the "pure" css file would be slightly faster simply because it doesn't need to parse php code. However, in a php file you can have more control over headers which may be more important(?).

Currently I'm doing a filemtime() check on a "trigger" file, and with some php voodoo writing a single compressed css file from it, combined with several other files in a defined group. This creates a file like css/groupname/301469778.css, which the php template catches and updates the html tags with the new file name. It seemed like the safest method, but I don't really like the server cache getting filled up with junk css files after several edits. I also don't bother doing this for small "helper" css files that are only loaded for certain pages.

  • If 99% of my output is generated by php anyways, what's the harm (if any) by using php to directly output css/js content? (assuming there are no php errors)
  • If using php, is it a good idea to mod_rewrite the files to use the css/js extension for any edge cases of browser misinterpretation? Can't hurt? Not needed?
  • Are there any separate guidelines/methods for css and javascript? I would assume that they would be equal.
  • Which is faster: A single css file with several @imports, or a php file with several readfile() calls?
  • What other ways does using php affect speed?
  • Once the file is cached in the browser, does it make a difference anymore?

I would prefer to use php with .htaccess because it is much simpler, but in the end I will use whatever method is best.

like image 863
Wesley Murch Avatar asked Apr 02 '11 08:04

Wesley Murch


People also ask

What is faster PHP or JavaScript?

Speed. In general, JavaScript executes faster than PHP on the same hardware. However, because JavaScript runs on the client, if the client machine is old and sluggish, that will have a knock-on effect on the execution time.

What can PHP do that JavaScript Cannot?

Key Difference between PHP and JavaScript PHP doesn't execute within the browser, whereas Javascript executes within the browser. PHP supports databases, whereas Javascript doesn't support databases. PHP accepts both upper cases and lower case variables, while Javascript doesn't.

Which is more secure PHP or JavaScript?

Security. On its own, PHP is usually more secure than JavaScript since it's a server-side scripting language. You can't access the code from the browser. Whereas almost anybody can see the code behind a web page in JavaScript.

When would you use PHP over JavaScript?

Javascript does the job for Both Front-end and Back-end. PHP is used mostly for Back-end purposes only. 2.


1 Answers

ok, so here are your direct answers:

  • no harm at all as long as your code is fine. The browser won't notice any difference.
  • no need for mod_rewrite. the browsers usually don't care about the URL (and often not even about the MIME type).
  • CSS files are usually smaller and often one file is enough, so no need to combine. Be aware that combining files from different directories affect images referenced in the CSS as they remain relative to the CSS URL
  • definitely readfile() will be faster as @import requires multiple HTTP requests and you want to reduce as much as possible
  • when comparing a single HTTP request, PHP may be slightly slower. But you loose the possibility to combine files unless you do that offline.
  • no, but browser caches are unreliable and improper web server config may cause the browser to unnecessarily re-fetch the URL.

It's impossible to give you a much more concrete answer because it depends a lot on your project details.

like image 67
Udo G Avatar answered Oct 06 '22 01:10

Udo G