What are the potential causes of the "headers already sent" error in PHP?
The "headers already sent" error in PHP occurs when the server tries to send headers (like cookies or redirects) to the client, but there has already been output sent to the browser before the headers are set. This can be caused by whitespace before the opening `<?php` tag, HTML output before `header()` function calls, or PHP `echo` or `print` statements before setting headers. To solve this issue, make sure there is no output sent to the browser before setting headers. Check for any whitespace or HTML content before PHP opening tags and remove them. Use output buffering functions like `ob_start()` and `ob_end_flush()` to buffer output and prevent headers from being sent prematurely.
<?php
ob_start(); // Start output buffering
// Your PHP code here
ob_end_flush(); // Flush output buffer and send output to the browser