What are some common methods for preserving variable values in PHP sessions?

When working with PHP sessions, it is common to need to preserve variable values across multiple pages or requests. One common method for achieving this is by storing the variables in the $_SESSION superglobal array. This array allows you to store and retrieve values throughout a user's session on your website.

// Start the session
session_start();

// Store a variable in the session
$_SESSION['username'] = 'JohnDoe';

// Retrieve the variable from the session
$username = $_SESSION['username'];

// Unset a variable from the session
unset($_SESSION['username']);