$random = rand();
$newRandom = 0;
echo "The random number is ".$random;
echo "<br>";
echo "<br>";
session_start(); // start a session
PHP.net wrote:To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
Solution call session_start(); before you do anything else.
$_SESSION['random'] = "$random";
if(isset($_SESSION['random'])){ // check if the random number is stored in the session
$newRandom += $random;
echo $newRandom;
}
Here you set $_SESSION['random'] to the newly generated random number, then you check to see if it's set (which of course it is you just set it) then you add the newly generated number to 0 (which is what you previously set $newRandom to) and expect it to not be the same. For all x: x+0 = x
You may try something along these lines:
<?php
session_start();
$rand = rand();
echo 'The random number is: '.$rand.'<br><br>';
if( isset($_SESSION['rand']) ) {
$_SESSION['rand'] += $rand;
} else {
$_SESSION['rand'] = $rand;
}
echo 'You running total is: '.$_SESSION['rand'];