Bird
0
0

Find the mistake in this code:

medium📝 Debug Q7 of 15
PHP - Sessions and Cookies
Find the mistake in this code:
<?php
session_start();
echo $_SESSION['name'];
$_SESSION['name'] = 'Anna';
?>
AMissing semicolon after session_start()
Bsession_start() is called twice
CSession variables cannot be strings
DTrying to echo before setting the session variable
Step-by-Step Solution
Solution:
  1. Step 1: Analyze order of operations

    The code tries to print $_SESSION['name'] before it is set.
  2. Step 2: Understand session variable initialization

    Since $_SESSION['name'] is not set yet, echo outputs nothing or warning.
  3. Final Answer:

    Trying to echo before setting the session variable -> Option D
  4. Quick Check:

    Set session vars before echoing them [OK]
Quick Trick: Assign session variables before reading them [OK]
Common Mistakes:
  • Echoing unset session variables
  • Ignoring variable initialization order
  • Expecting default values for unset keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes