How can output buffering be used as an alternative to header() for redirection in PHP?

When headers are already sent to the browser, using header() for redirection in PHP can cause errors. Output buffering can be used as an alternative to buffer the output before sending it to the browser, allowing us to set headers at any point in the script.

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

// Perform any PHP operations here

// Redirect to a new page after output buffering has started
header('Location: newpage.php');

ob_end_flush();
// Flush the output buffer and send it to the browser
?>