Bird
0
0

What is wrong with this PHP code for state management?

medium📝 Debug Q14 of 15
PHP - Sessions and Cookies
What is wrong with this PHP code for state management?
<?php
$_SESSION['count'] = 1;
echo $_SESSION['count'];
?>
ASession variables cannot store numbers
BUsing echo instead of print
CMissing session_start() before using $_SESSION
DVariable name 'count' is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Check session usage rules

    PHP requires session_start() before accessing $_SESSION variables.
  2. Step 2: Identify missing session_start()

    The code sets and echoes $_SESSION['count'] without session_start(), causing errors.
  3. Final Answer:

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

    session_start() needed before $_SESSION [OK]
Quick Trick: Always call session_start() before $_SESSION use [OK]
Common Mistakes:
  • Thinking echo must be replaced
  • Believing variable names are restricted
  • Assuming sessions can't store numbers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes