Bird
0
0

Consider this PHP code:

medium📝 Predict Output Q5 of 15
PHP - Sessions and Cookies
Consider this PHP code:
setcookie('lang', 'en', time() + 3600);
echo isset($_COOKIE['lang']) ? $_COOKIE['lang'] : 'No language set';

What will it output on the first page load?
ANotice: Undefined index: lang
Ben
CNo language set
DEmpty string
Step-by-Step Solution
Solution:
  1. Step 1: Recognize cookie availability timing

    Cookies set with setcookie() are not available in $_COOKIE until the next page load.
  2. Step 2: Evaluate the ternary condition

    Since $_COOKIE['lang'] is not set yet, the else part 'No language set' will be printed.
  3. Final Answer:

    No language set -> Option C
  4. Quick Check:

    First load cookie = Not available yet [OK]
Quick Trick: Cookies appear in $_COOKIE only after next request [OK]
Common Mistakes:
  • Expecting cookie value immediately
  • Ignoring isset() check
  • Confusing setcookie() with $_COOKIE availability

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes