How can the issue of setting session variables to empty arrays in PHP be elegantly resolved to avoid errors and maintain proper data handling practices?

Issue: The issue of setting session variables to empty arrays in PHP can be elegantly resolved by checking if the session variable is already set before assigning an empty array to it. This helps avoid errors and ensures proper data handling practices.

// Check if the session variable is already set before assigning an empty array
if (!isset($_SESSION['my_array'])) {
    $_SESSION['my_array'] = [];
}