What potential pitfalls should be avoided when using setcookie function in PHP to prevent errors related to modifying header information?

When using the setcookie function in PHP, it is important to avoid sending any output before calling the function to prevent errors related to modifying header information. To solve this issue, make sure to call the setcookie function before any HTML or text output is sent to the browser.

<?php
// Correct way to set a cookie in PHP
if (!headers_sent()) {
    setcookie("user", "John Doe", time() + 3600, "/");
}
?>