How can the "headers already sent" error be resolved when including the DOCTYPE declaration in a PHP file?

When including the DOCTYPE declaration in a PHP file, the "headers already sent" error can be resolved by ensuring that there is no whitespace or output before the DOCTYPE declaration. This error occurs when PHP tries to send headers (such as the DOCTYPE declaration) after output has already been sent to the browser. To fix this issue, make sure there is no output, including whitespace, before the DOCTYPE declaration in the PHP file.

<?php
ob_start(); // Start output buffering
?>
<!DOCTYPE html>
<html>
<head>
    <title>Example Page</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>
<?php
ob_end_flush(); // Flush output buffer
?>