Posting Guidelines

Please provide links that are useful resources and will help out a community of professionally minded web developers.

Please feel free to ask for help or advice, but if you do be as thorough as possible about your current problem and explain what you're trying to achieve and specifically what can be done to help! Please don't beg for advice or help.

Just follow the comment for how to format the filenames. You can use something like Fabien4's solution involving time stamping, however I like to look at the last modification to prevent forcing everyone to download new resources even when there is no changes:

The addition of a query string is not a "hack" and is supported by all browsers I am aware of. The example you show here would cause undue amount of stress on the server as the last modified date is looked up with every request. In fact, you'd have to look up the modified date of every css and js file you want to load. SLOW!

The best solution I have found is to set a constant setting for js/css script version and increment it whenever you want them pulled.

To Fabien, using single quotes for HTML attributes is shameful! You even missed quotes for the rel attribute. Furthermore, "type" is not required, only "rel".

The addition of a query string is not a "hack" and is supported by all browsers I am aware of.

URLs with a query string are supported by all modern browsers but they are not supported by most proxies. (Source)

The example you show here would cause undue amount of stress on the server as the last modified date is looked up with every request. In fact, you'd have to look up the modified date of every css and js file you want to load. SLOW!

Agreed, this is more overhead than necessary. A plain number that you increment is much better.

Also for extra speed, use Apache config instead of htaccess and set AllowOverride None.

Interesting! I didn't realize some proxies did that. The 3rd point is especially interesting... basically recommending to set a header that proxies should NOT cache css/js.

Be aware of issues with proxy caching of JS and CSS files.
Some public proxies have bugs that do not detect the presence of the Content-Encoding response header. This can result in compressed versions being delivered to client browsers that cannot properly decompress the files. Since these files should always be gzipped by your server, to ensure that the client can correctly read the files, do either of the following:

Set the the Cache-Control header to private. This disables proxy caching altogether for these resources. If your application is multi-homed >around the globe and relies less on proxy caches for user locality, this might be an appropriate setting.
Set the Vary: Accept-Encoding response header. This instructs the proxies to cache two versions of the resource: one compressed, and one >uncompressed. The correct version of the resource is delivered based on the client request header. This is a good choice for applications that are >singly homed and depend on public proxies for user locality.

It is cached per file per request... that is, if you call filemtime on the same file twice within the same HTTP request, it will not check it twice unless you call clearstatcache(). Unfortunately that doesn't help at all when you are only calling each include once per request.

I did exaggerate, however. It is not that slow.

0.00216 seconds when checking 50 files.
0.00044 seconds when checking 10 files. This one is a pretty realistic number of files. 0.00044 is still fast, but considering all it is an anti-caching mechanism it's kind of a lot of time--especially since it is disk time. For your personal website it probably wouldn't matter, but for a high traffic site it would.

I've made a little C program to try and profile the problem.
Basically, it can either call stat(), which will return a file's modification time, time(), which will return the current time, or assign some dummy value. It will then optionally print the result.

I've tested it with 1 file, and with 1000 files.

Calling no function is, unsurprisingly, very fast. (In fact, most of the time is spent starting the program.)

Calling stat() is slightly slower than calling time(), but by a very small margin. It could well be due to the fact that stat() sets a lot of values, not just the modification time. I get roughly 1 to 2 microseconds per call.

In summary, I can confirm that reading a file's modification time is not slowed down by the hard disk.

Interesting. I agree with you that it should be able to be cached and should be fast, but I haven't seen it in practice. I am running this on Win7 however. Perhaps linux handles this differently.

Would you run the test below and let me know the performance on your end? I realize the machine we have will play a role but if there orders of magnitude difference then perhaps it is different because of our file systems.

I just choose the coding style that seems the most readable to me. You don't agree with me, just like different programmers can't agree on indentation styles. That doesn't mean I'm wrong, or that you have the One True Way.

HTML is regularly read and written by humans. Being consistent helps with that. XML is often used for configuration files (read by humans) as well as serialization/deserialization of data (not read by humans except maybe when setting up the system).

Please show me a single major website that uses no quotes around attributes or all uppercase tags. A few might have a few singles quotes or unclosed meta tags, but the majority are double quotes and lowercase.

There are plenty of gotchas when omitting quotes, like when the attribute contains a space (in the case of multiple classes or a file name with a space). And like terminalcomputer said, it's better to be consistent where possible.

And funny enough I prefer double quotes for seemingly the same reason you prefer single quotes - when outputting via PHP. I use single quotes for most PHP output as it's clearer where the variables are. I would do your example like this (BTW the type attribute is unnecessary):

echo '<link href="'.$name.'?date='.$time.'" rel="stylesheet">';
// or more likely in a template like this:
<link href="<?=$name?>?date=<?=$time?>" rel="stylesheet">

But I agree with the general sentiment of using HTML - XHTML dies years ago. I tend to omit optional closing tags like <td>, <p> and <li> as it looks cleaner IMO.

AFAIK, getlastmod() will give you the date of last modification of the .php file. That means, if you modify the .php file, you'll needlessly force reloading all images; worse, if you modify a .png (and not the .php), the modification won't be detected, and the browser might keep the old version.