'How to get previously stored $_SESSION value?

I am here storing the randomly generated value from rand() into $_SESSION['previous_rand']

. I am also echo both the current generated rand() and previously stored in $_SESSION['previous_rand']

but it showing the current generated rand() into the previously generated rand() field.

How can i make it show the previously generated rand() correctly?

<?php

session_start();

$rand = rand(1000,9999);
$_SESSION['previous_rand'] = $rand;


echo "Current generated RAND: " . $rand;
echo "<br>";
echo "Previously generated RAND: " . $_SESSION['previous_rand'];
  
?>
php


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source