How can session variables be properly initialized to prevent errors like "Invalid Argument supplied for foreach()"?

Session variables should be properly initialized by checking if they are set before using them in a foreach loop to prevent errors like "Invalid Argument supplied for foreach()". This can be done by using the isset() function to check if the session variable is set and not empty before iterating over it with foreach.

// Initialize session
session_start();

// Check if the session variable is set and not empty before using it in a foreach loop
if(isset($_SESSION['your_session_variable']) && !empty($_SESSION['your_session_variable'])) {
    foreach($_SESSION['your_session_variable'] as $item) {
        // Your code here
    }
}