Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q5 of 15
PHP - Sessions and Cookies
What will be the output of the following PHP code?
session_start();
$_SESSION['username'] = 'Bob';
session_unset();
echo isset($_SESSION['username']) ? $_SESSION['username'] : 'Guest';
ABob
BGuest
CEmpty string
DError message
Step-by-Step Solution
Solution:
  1. Step 1: session_start()

    Starts the session and initializes $_SESSION superglobal.
  2. Step 2: Assign 'Bob' to $_SESSION['username']

    Stores the value in session data.
  3. Step 3: session_unset()

    Removes all session variables, so $_SESSION['username'] is unset.
  4. Step 4: Check isset()

    Since $_SESSION['username'] is unset, isset() returns false.
  5. Final Answer:

    Guest is echoed because the ternary operator falls back to 'Guest'. -> Option B
  6. Quick Check:

    session_unset() clears variables, so no username [OK]
Quick Trick: session_unset() clears $_SESSION variables [OK]
Common Mistakes:
  • Assuming session_unset() destroys the session
  • Expecting 'Bob' to be printed after session_unset()
  • Confusing session_unset() with session_destroy()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes