Bird
0
0

How can you safely remove a specific session variable named cart without destroying the entire session?

hard📝 Application Q9 of 15
PHP - Sessions and Cookies
How can you safely remove a specific session variable named cart without destroying the entire session?
A$_SESSION = array();
Bsession_destroy();
Cunset($_SESSION['cart']);
Ddelete $_SESSION['cart'];
Step-by-Step Solution
Solution:
  1. Step 1: Identify method to remove one session variable

    Using unset() removes a single key from the $_SESSION array.

  2. Step 2: Compare with other options

    session_destroy() ends the whole session, $_SESSION = array() clears all variables, and delete is invalid syntax.

  3. Final Answer:

    unset($_SESSION['cart']); -> Option C
  4. Quick Check:

    Remove one session key = unset() [OK]
Quick Trick: Use unset() to delete one session variable safely [OK]
Common Mistakes:
  • Using session_destroy() to remove one variable
  • Assigning empty array clears all session data
  • Using invalid delete keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes