How can session variables be accessed and destroyed in PHP?

Session variables in PHP can be accessed using the $_SESSION superglobal array. To destroy a session variable, you can use the unset() function or the session_unset() function. It is important to unset session variables when they are no longer needed to free up memory and maintain security.

// Accessing session variables
session_start();
$_SESSION['username'] = 'JohnDoe';
echo $_SESSION['username'];

// Destroying a session variable
unset($_SESSION['username']);
// OR
session_unset();