How can sessions be used to store and retrieve data for calculations on the same page in PHP?

To store and retrieve data for calculations on the same page in PHP, you can use sessions to temporarily store the data. You can set session variables with the data you need to calculate, perform the calculations on the same page, and then retrieve the results from the session variables.

<?php
session_start();

// Set session variables with data for calculations
$_SESSION['num1'] = 10;
$_SESSION['num2'] = 5;

// Perform calculations
$result = $_SESSION['num1'] + $_SESSION['num2'];

// Retrieve results from session variables
echo "Result of calculation: " . $result;
?>