How can the fopen() function affect the output buffer in PHP?

When using the fopen() function in PHP to open a file for writing, it can affect the output buffer by causing unexpected behavior such as output being sent before headers are set. To prevent this issue, you can use the ob_start() function to start output buffering before opening the file, and then use ob_end_flush() to flush the output buffer after writing to the file.

<?php
ob_start(); // start output buffering

$file = fopen("output.txt", "w");
fwrite($file, "Hello, World!");
fclose($file);

ob_end_flush(); // flush the output buffer
?>