<?php
$filename = "index.html";
ob_start();
// All your code goes here
$content = ob_get_contents();
// If you get rid of this it will also output the data after running the script
ob_end_clean();
$handle = fopen($filename, 'w');
fwrite($handle, $content);
?>
Will that work?
It just starts output buffering, then once its done with all the code, grabs the contents and puts it into a variable, then cleans out the output buffer. Then stores the variable(the html) to a file, index.html.