Skip to content
PHP Logo

What is the recommended way to unset variables in a PHP session without destroying the entire session?

To unset variables in a PHP session without destroying the entire session, you can use the `unset()` function to remove specific variables from the `$_SESSION` superglobal array. This allows you to selectively remove only the variables you want without affecting the rest of the session data.

php
// Start the session
session_start();

// Unset specific variables in the session
unset($_SESSION['variable1']);
unset($_SESSION['variable2']);