What is the behavior of the setcookie function in PHP when checking for the existence of a cookie?

The setcookie function in PHP does not directly check for the existence of a cookie before setting a new one, which can lead to unexpected behavior or errors if not handled properly. To avoid overwriting existing cookies, you can check if a cookie with the same name already exists using the isset function before calling setcookie.

if(!isset($_COOKIE['cookie_name'])) {
    setcookie('cookie_name', 'cookie_value', time() + 3600, '/');
}