<?php
start:
	
echo <<< stop1
<html>
<body>
stop1;
echo "<title>Sum Game</title>";
error_reporting(E_ALL | E_NOTICE);  /*error report*/
$one = rand(1,1000); /*generate random number for cauculation*/
$two = rand(1,1000); /*generate random number for cauculation*/

echo "<p>$one + $two=</p>"; /*show formula*/
echo <<<stop2
<form method = "POST">
<input name="num1" value=$one type="hidden"> <!--store the random number for future calculation-->
<input name="num2" value=$two type="hidden"> <!--store the random number for future calculation-->
<input name="input"> <!--box for input-->
<button name="action" value="submit" type="submit">Done!</br></button><!--button for summit-->
</form>
stop2;

if(($_SERVER['REQUEST_METHOD'] !== 'POST') || @$_POST['input'] === ""){ /*check if the POST exist or the answer is NULL*/
	GOTO end; /*if not, skip all the out put*/
}

$ans = $_POST['num1']+$_POST['num2']; /*calculate the correct answer*/

if(!(is_numeric($_POST['num1'])&&is_numeric($_POST['num2'])&&is_numeric($_POST['input'])))
	{echo "wrong input: \"",htmlspecialchars($_POST['input'],ENT_QUOTES),"\"";
	die();
}

result_output:

if($ans==$_POST['input']){
	echo "<p>You're right!</p>", $_POST['num1'], "+", $_POST['num2'], "=", $ans;
}elseif ($ans!=$_POST['input']){
	echo "<p>That's wrong :( </p> <br>", $_POST['num1'], "+", $_POST['num2'], "=", $ans;
	echo "<br>Your input is", htmlspecialchars($_POST['input'],ENT_QUOTES), ", right answer is", $ans;
	echo "<br>", abs($_POST['input']-$ans), " difference from the correct answer";
}

end:
	
echo '<br><br><a href="../index.html">home page</a>';
echo <<< stop1
	</html>
	</body>
stop1;