I have written a simple code in page test_cookie.php to work with cookies.
if(isset($_GET['data'])) {
setcookie('test_cookie', $_GET['data'], time()+60*60*24*30);
}
echo 'Cookie name is: ' . $_COOKIE['test_cookie'];
Now once cookie is set to something, and I set the value to something else (using $_GET), then the content value doesn’t change quickly. I have to refresh it again, if a new value comes.
Here is a picture example:

Yes – just set it explicitly when you call
setcookie():The reason your code doesn’t work “quickly” is because
$_COOKIEcontains cookies sent by the browser in the current request, whilesetcookie()sends a cookie header to the broswer in this response. Since request comes before response, you need to otherwise wait for the next request to see the cookie you just set. The above code gets around this by pretending that the cookie we have just sent was received in this request.