When the following code is run for the first time, it does not echo out the \"var1\" value but it does on browser refresh (f5), Why? As i understand when the browser sends t
I believe this has to do with the way $_COOKIE is populated. According to the reference material, it comes from any cookies sent by the client. The cookie is sent in the HTTP header when you make a request for a page, so it makes sense that it only works the second time. The first time around, you are setting the cookie (client doesn't know about it yet to send it along with the first request). When you refresh, the cookie has been set by the first page load, and the client thus sends the cookie with the request, which PHP sees and populates into the $_COOKIE variable.
http://php.net/manual/en/features.cookies.php