How can PHP sessions be effectively used to store and display multiple product IDs added to a shopping cart dynamically?

To store and display multiple product IDs added to a shopping cart dynamically using PHP sessions, you can store the product IDs in an array within the session variable. This allows you to easily add, remove, and display the products in the shopping cart as needed.

<?php
session_start();

// Check if the product ID is passed through a form or URL parameter
if(isset($_GET['product_id'])) {
    $product_id = $_GET['product_id'];
    
    // Add the product ID to the shopping cart array in the session
    $_SESSION['cart'][] = $product_id;
}

// Display the products in the shopping cart
if(isset($_SESSION['cart'])) {
    echo "Products in your shopping cart:<br>";
    foreach($_SESSION['cart'] as $product_id) {
        echo "Product ID: " . $product_id . "<br>";
    }
}
?>