Another way to save all output to a file using ob_start() (before any output) with callback function:
ob_start(function ($buffer) {
file_put_contents('file.txt', $buffer, FILE_APPEND);
return $buffer; // remove this line if you dont want to show the output
});