Bird
0
0

Why are cookies not immediately available in the $_COOKIE array after calling setcookie() in the same PHP script?

hard📝 Conceptual Q10 of 15
PHP - Sessions and Cookies
Why are cookies not immediately available in the $_COOKIE array after calling setcookie() in the same PHP script?
ABecause <code>$_COOKIE</code> is read-only and cannot be changed
BBecause cookies are sent to the browser and only returned on the next request
CBecause <code>setcookie()</code> does not actually set cookies
DBecause cookies require JavaScript to be accessible
Step-by-Step Solution
Solution:
  1. Step 1: Understand HTTP cookie flow

    Cookies are sent as headers to the browser, which stores them and sends them back on the next HTTP request.
  2. Step 2: Explain why $_COOKIE is not updated immediately

    Since the current script runs before the browser receives the cookie, $_COOKIE does not contain the new cookie yet.
  3. Final Answer:

    Because cookies are sent to the browser and only returned on the next request -> Option B
  4. Quick Check:

    Cookie availability delay = Next request [OK]
Quick Trick: Cookies appear in $_COOKIE only after browser sends them back [OK]
Common Mistakes:
  • Thinking setcookie() sets $_COOKIE immediately
  • Believing $_COOKIE can be modified directly
  • Assuming JavaScript is needed for cookie access

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes