Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - Sessions and Cookies
Identify the error in this PHP code snippet:
<?php
$_SESSION['user'] = 'Alice';
echo $_SESSION['user'];
?>
ACannot assign string to $_SESSION
BIncorrect variable name, should be $SESSION
CMissing session_start() before using $_SESSION
DEcho statement syntax is wrong
Step-by-Step Solution
Solution:
  1. Step 1: Check session initialization

    The code uses $_SESSION without calling session_start() first, which is required.
  2. Step 2: Verify other options

    Variable name $_SESSION is correct, strings can be assigned, and echo syntax is valid.
  3. Final Answer:

    Missing session_start() before using $_SESSION -> Option C
  4. Quick Check:

    session_start() needed before $_SESSION = A [OK]
Quick Trick: Always call session_start() before $_SESSION usage [OK]
Common Mistakes:
  • Forgetting session_start() causes undefined $_SESSION
  • Confusing $_SESSION with $SESSION
  • Thinking echo syntax is incorrect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes