I am attempting to make a random number guessing game in PHP. The guesses are entered into the $_GET var in the address bar after a ?. However, each time the enter key is pressed the random number itself changes. How can I keep this random number intact until the game is lost or won.
<!DOCTYPE html>
<html>
<head>
<title>Guess Gaming</title>
</head>
<body>
<h1>Guess the correct Number</h1>
<p>
<?php
$randomNum = rand(0, 150);
if(empty($_GET["guess"])){
echo "Missing guess parameter.";
} elseif(!empty($_GET["guess"])){
if(is_numeric($_GET["guess"])){
if($_GET["guess"] === $randomNum){
echo "Congratulations-- You are right";
} elseif($_GET["guess"] < $randomNum){
echo "Your guess is too low.";
} elseif($_GET["guess"] > $randomNum){
echo "Your guess is too high.";
}
} else {
echo $_GET["guess"]." isn't numeric.";
}
}
?>
</p>
<p>
<?php
echo $randomNum;
?>
</p>
</body>
</html>
$randomNum
into session, then just use that session when checking. When he will be right, reset that session with new number.