What are best practices for passing data like arrays between PHP files using sessions?

When passing data like arrays between PHP files using sessions, it is important to serialize the array before storing it in the session and unserialize it when retrieving it. This ensures that the array maintains its structure and data integrity when being passed between files. Additionally, always check if the session is started before accessing or setting session variables to avoid errors.

// Start the session
session_start();

// Serialize the array before storing it in the session
$data = array('foo' => 'bar', 'baz' => 'qux');
$_SESSION['data'] = serialize($data);

// Unserialize the array when retrieving it
$data = unserialize($_SESSION['data']);

// Check if the session is started before accessing session variables
if (isset($_SESSION['data'])) {
    $data = unserialize($_SESSION['data']);
}