What is the role of sessions in maintaining data across requests in PHP?
Sessions in PHP play a crucial role in maintaining data across requests by storing data on the server and associating it with a unique session ID that is passed to the client. This allows the server to retrieve the data for each subsequent request made by the client, ensuring continuity and persistence of data throughout the user's session.
// Start a session
session_start();
// Store data in the session
$_SESSION['username'] = 'JohnDoe';
// Retrieve data from the session
$username = $_SESSION['username'];
// Destroy the session when it is no longer needed
session_destroy();