How can the output of a PHP function be redirected to a file instead of being displayed in the browser?

To redirect the output of a PHP function to a file instead of displaying it in the browser, you can use output buffering along with file handling functions. Start by turning on output buffering using ob_start() before calling the function whose output you want to redirect. Then, use ob_get_contents() to capture the output and file_put_contents() to write it to a file. Finally, clear the output buffer using ob_end_clean().

<?php
ob_start();
// Call the PHP function whose output you want to redirect
echo "Hello, World!";
$output = ob_get_contents();
ob_end_clean();

file_put_contents('output.txt', $output);
?>