How can multiple values be stored in a session sequentially, such as in an online shopping cart?

To store multiple values in a session sequentially, such as in an online shopping cart, you can use an array to hold all the items. Each item can be added to the array as needed, and the array can be stored in the session variable. This way, you can easily access and manipulate all the items in the shopping cart.

// Start the session
session_start();

// Check if the shopping cart array exists in the session
if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = array(); // Initialize an empty array
}

// Add an item to the shopping cart
$item = array(
    'id' => 1,
    'name' => 'Product Name',
    'price' => 10.99
);
$_SESSION['cart'][] = $item;

// Display all items in the shopping cart
foreach ($_SESSION['cart'] as $item) {
    echo $item['name'] . ' - $' . $item['price'] . '<br>';
}