Bird
0
0

What is the issue with this PHP code snippet?

medium📝 Debug Q6 of 15
PHP - Sessions and Cookies
What is the issue with this PHP code snippet?
echo $_SESSION['username'];
session_start();
$_SESSION['username'] = 'Charlie';
ASession variable is assigned after echoing
BSession is accessed before calling session_start()
CSession variable name is invalid
DNo issue; code works correctly
Step-by-Step Solution
Solution:
  1. Step 1: Identify session start

    session_start() must be called before accessing $_SESSION.
  2. Step 2: Analyze code order

    The code echoes $_SESSION['username'] before starting the session, which causes an error or empty output.
  3. Final Answer:

    Session is accessed before calling session_start() -> Option B
  4. Quick Check:

    Always call session_start() first [OK]
Quick Trick: Call session_start() before using $_SESSION [OK]
Common Mistakes:
  • Accessing $_SESSION before session_start()
  • Assigning session variables after output
  • Assuming session variables exist without initialization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes