How can PHP sessions be used to maintain variable values across different files?

To maintain variable values across different files in PHP, you can use sessions. Sessions allow you to store variables that can be accessed across multiple pages during a user's visit to a website. By starting a session and setting session variables, you can maintain the values of these variables throughout the user's session.

// File 1: start_session.php
session_start();

// Set session variables
$_SESSION['username'] = 'JohnDoe';
$_SESSION['email'] = 'johndoe@example.com';

// File 2: display_session.php
session_start();

// Access session variables
echo 'Username: ' . $_SESSION['username'] . '<br>';
echo 'Email: ' . $_SESSION['email'];