Use output buffers. At the beginning of your script, do:
ob_start();
at the end of your script do:
$source = ob_get_contents();
ob_end_flush();
$fp = fopen("file.html", "w");
if (is_resource($fp)) {
fwrite($fp, $source);
fclose($fp);
} else {
echo "could not save cache file!";
}
Note that you may also want to take a look at PEAR's Cache modules (though somewhat inefficient IMHO). Look at Cache.php supplied with your PHP installation (default directory is /usr/local/lib/php on RedHat from source). Note that PHP required --with-pear enabled at compile time for `make install' to have installed it.