Guess the Number
// Generate a random number between 1 and 100
$number = rand(1, 100);
// Check if the form has been submitted
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
// Retrieve the guessed number
$guess = $_POST[“guess”];
// Validate the guessed number
if (!empty($guess) && is_numeric($guess)) {
$guess = intval($guess);
// Compare the guessed number with the generated number
if ($guess == $number) {
echo ”
Congratulations! You guessed the correct number!
“;
echo ”
“;
} elseif ($guess < $number) {
echo ”
Too low! Try a higher number.
“;
} else {
echo ”
Too high! Try a lower number.
“;
}
} else {
echo ”
Please enter a valid number.
“;
}
}
?>