How can sessions be used to maintain variables across multiple PHP files?

Sessions can be used to maintain variables across multiple PHP files by storing the variable values in the $_SESSION superglobal array. This allows the variables to persist across different pages within the same session. To use sessions, you need to start the session with session_start() at the beginning of each PHP file where you want to access the session variables.

// File 1: start_session.php
session_start();
$_SESSION['username'] = 'John';

// File 2: display_session.php
session_start();
echo $_SESSION['username']; // Output: John