Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - Superglobals and Web Context
Identify the error in this PHP code snippet:
<?php
echo $_SESSION['user'];
?>

Assuming session_start() was not called earlier.
AUndefined index 'user' error
BSession not started error or warning
CNo error, code works fine
DSyntax error due to missing semicolon
Step-by-Step Solution
Solution:
  1. Step 1: Understand $_SESSION behavior

    $_SESSION is always available as an empty array if no session data is loaded; session_start() initializes session handling but does not set keys.
  2. Step 2: Identify the specific error

    Since 'user' key is not set, accessing $_SESSION['user'] produces a "Notice: Undefined index: user" (undefined index error).
  3. Final Answer:

    Undefined index 'user' error -> Option A
  4. Quick Check:

    Accessing unset $_SESSION['user'] = undefined index [OK]
Quick Trick: Always call session_start() before using $_SESSION [OK]
Common Mistakes:
  • Assuming $_SESSION works without session_start()
  • Confusing undefined index with session error
  • Missing semicolon is not the issue here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes