How can session storage be utilized to store and retrieve information from a shopping cart in PHP?

Session storage can be utilized in PHP to store and retrieve information from a shopping cart by storing the cart items in the session variable. This allows the shopping cart data to persist across different pages and visits to the website. To implement this, you can store the cart items in the session variable using the $_SESSION superglobal and retrieve them when needed.

// Start the session
session_start();

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

// Retrieve items from the shopping cart
if(isset($_SESSION['cart'])) {
    foreach($_SESSION['cart'] as $item) {
        echo $item['name'] . ' - $' . $item['price'] . '<br>';
    }
}