Furthermore, assuming you have output compression enabled, the resulting collated file will almost certainly be smaller than the sum of the individual files, due to the way compression algorithms work.
Here's how to achieve this in PHP:
First, you'll want to request a collection of JS files to be written:
$script_tag = fetch_script_cache(array('global.js', 'widget.js', 'search.js', 'map.js'));
Now we define our function
The first thing we do is generate an cache index ($key) based on all of the filenames. The we go through all of our files to find out which was the most recently modified, and check if that is more recent than the age of the cache. If it is, the we need to update the cache. The cache file is simply a concatenation of all the files in the list (and we add a line break "\r\n" to avoid any complications).
Our return value is simply a well-formed script tag pointing at the output cache file. The should be outputted in the head of the document, and you're all done!
All scripts are Public Domain.