Why is it important to call header() before any actual output is sent in PHP scripts?

Calling header() before any actual output is sent in PHP scripts is important because headers must be sent before any content. If headers are sent after content, it can lead to errors such as "Headers already sent" or unexpected behavior in the script. To prevent this issue, always call header() at the beginning of the script before any HTML, whitespace, or echo statements.

<?php
// Always call header() before any actual output is sent
header("Content-Type: text/html");

// Rest of the PHP code goes here
?>