Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8350985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:29:30+00:00 2026-06-09T08:29:30+00:00

Why don’t people make .php files for their CSS and JavaScript files? Adding <?php

  • 0

Why don’t people make .php files for their CSS and JavaScript files?

Adding <?php header("Content-type: text/javascript; charset: UTF-8"); ?> to the file makes it readable by browsers, and you can do the same thing to css files by setting the Content-type property to text/css.

It lets you use all the variables of PHP and methods into the other languages. Letting you, as an example, change the theme main colors depending on user preferences in css, or preloading data that your javascript can use on document load.

Are there bad sides of using this technique?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:29:32+00:00Added an answer on June 9, 2026 at 8:29 am

    People do it more often than you think. You just don’t get to see it, because usually this technique is used in combination with URL rewriting, which means the browser can’t tell the difference between a statically-served .css file and a dynamic stylesheet generated by a PHP script.

    However, there are a few strong reasons not to do it:

    • In a default configuration, Apache treats PHP script output as ‘subject to change at any given time’, and sets appropriate headers to prevent caching (otherwise, dynamic content wouldn’t really work). This, however, means that the browser won’t cache your CSS and javascript, which is bad – they’ll be reloaded over the network for every single page load. If you have a few hundred page loads per second, this stuff absolutely matters, and even if you don’t, the page’s responsivity suffers considerably.
    • CSS and Javascript, once deployed, rarely changes, and reasons to make it dynamic are really rare.
    • Running a PHP script (even if it’s just to start up the interpreter) is more expensive than just serving a static file, so you should avoid it unless absolutely necessary.
    • It’s pretty damn hard to make sure the Javascript you output is correct and secure; escaping dynamic values for Javascript isn’t as trivial as you’d think, and if those values are user-supplied, you are asking for trouble.

    And there are a few alternatives that are easier to set up:

    • Write a few stylesheets and select the right one dynamically.
    • Make stylesheet rules based on class names, and set those dynamically in your HTML.
    • For javascript, define the dynamic parts inside the parent document before including the static script. The most typical scenario is setting a few global variables inside the document and referencing them in the static script.
    • Compile dynamic scripts into static files as part of the build / deployment process. This way, you get the comfort of PHP inside your CSS, but you still get to serve static files.

    If you want to use PHP to generate CSS dynamically after all:

    • Override the caching headers to allow browsers and proxies to cache them. You can even set the cache expiration to ‘never’, and add a bogus query string parameter (e.g. <link rel="stylesheet" type="text/css" href="http://example.com/stylesheet.css?dummy=121748283923">) and change it whenever the script changes: browsers will interpret this as a different URL and skip the cached version.
    • Set up URL rewriting so that the script’s URL has a .css extension: some browsers (IE) are notorious for getting the MIME type wrong under some circumstances when the extension doesn’t match, despite correct Content-Type headers.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
Don't think that I'm mad, I understand how php works! That being said. I
Don't know why people do not practice AJAX implementation for authentication systems. Is it
I don't know why this double json response are not successful: [{first_content:content,...}][{second_content:content,...}] So i
Don't exactly know what to search for: If i edit text in vim it
I don't have much PHP experience and I want to know how to best
Don't know why I can't reply to people on here with only a small
Don't understand why #include <Header.h> is not compiling while #include Header.h is compiling with
Don't know what the term is called (substitution?), but in python if you type
don't know if the title describes anything about what I'm trying to say but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.