How can multiple values be stored in a session in PHP for later retrieval and calculation?
To store multiple values in a session in PHP for later retrieval and calculation, you can use an associative array to hold all the values you want to store. You can then serialize the array and store it in the session. When you need to retrieve the values, you can unserialize the array from the session and access the individual values as needed.
<?php
// Start the session
session_start();
// Store multiple values in an associative array
$data = array(
'value1' => 10,
'value2' => 20,
'value3' => 30
);
// Serialize and store the array in the session
$_SESSION['data'] = serialize($data);
// Retrieve the serialized array from the session
$stored_data = unserialize($_SESSION['data']);
// Access individual values
$value1 = $stored_data['value1'];
$value2 = $stored_data['value2'];
$value3 = $stored_data['value3'];
// Perform calculations or operations with the retrieved values
$total = $value1 + $value2 + $value3;
// Output the result
echo "Total: " . $total;
?>