How does the use of output buffering in PHP affect the setting of cookies and headers?

When using output buffering in PHP, headers and cookies must be set before any output is sent to the browser. If headers or cookies are set after output has already been sent, PHP will throw an error. To solve this issue, you can use output buffering to capture all output and then set headers and cookies before flushing the output to the browser.

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

// Set cookies and headers
setcookie('cookie_name', 'cookie_value', time() + 3600);
header('Location: new_page.php');

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