How can understanding the concept of output buffering help prevent header modification errors in PHP scripts?

When output buffering is not used in PHP scripts, headers are sent to the browser as soon as they are generated. This can cause issues if headers need to be modified later in the script. By enabling output buffering, headers are stored in a buffer until the script has finished executing, allowing for modifications to be made without errors.

<?php
ob_start();

// Code that generates headers

// Modify headers if needed
header('Content-Type: application/json');

ob_end_flush();
?>