Addition
Introduction
Use PHP1 to generate two random numbers.
Display the numbers and ask the user to enter the sum of the numbers.
Check if the user's answer is correct and display the result with
PHP2.
Code - PHP1
<?php
// generate two random numbers
$number1 = rand(1, 10);
$number2 = rand(1, 10);
// display the HTML form
echo "<form action='index.php' method='post'>";
echo "$number1 + $number2 = ";
echo "<input type='text' name='answer' />";
// pass the numbers to the next page
echo "<input type='hidden' name='number1' value='$number1' />";
echo "<input type='hidden' name='number2' value='$number2' />";
echo "<input type='submit' />";
?>
Code - PHP2
<?php
// receive the numbers and the answer by user
// store in variables $number1, $number2, $answer
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
$answer = $_POST['answer'];
$correct = $number1 + $number2;
// check if the answer is correct
if ($answer == $correct) {
echo "Correct!";
} else {
echo "Wrong!";
}
?>
Task
Create a PHP script that generates three random numbers between 1 and 10.
Display the numbers and ask the user to enter the sum of the numbers.
Check if the user's answer is correct and display the result.