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.
mod_rewrite
the files to use the css/js extension for any edge cases of browser misinterpretation? Can't hurt? Not needed?@imports
, or a php file with several readfile()
calls?I would prefer to use php with .htaccess because it is much simpler, but in the end I will use whatever method is best.
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.
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.
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.
Javascript does the job for Both Front-end and Back-end. PHP is used mostly for Back-end purposes only. 2.
ok, so here are your direct answers:
It's impossible to give you a much more concrete answer because it depends a lot on your project details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With