What is the purpose of using ob_start() at the beginning of a PHP file and how does it relate to header() function calls?

When using the header() function in PHP to send HTTP headers, it is important to call ob_start() at the beginning of the file to buffer the output. This ensures that headers can be set even after content has been sent to the browser. Without ob_start(), attempting to call header() after content has been sent will result in an error.

<?php
ob_start();

// Your PHP code here

header("Location: https://www.example.com");
exit;

ob_end_flush();
?>