What are the potential consequences of headers being sent before setting a cookie in PHP?

Sending headers before setting a cookie in PHP can lead to an error where the cookie cannot be set properly. To solve this issue, make sure to set the cookie before any headers are sent to the browser.

<?php
// Set the cookie before any headers are sent
setcookie("user", "John Doe", time() + 3600, "/");
// Send headers after setting the cookie
header("Location: index.php");
?>