有些帮助我使用if else语句的用户输入

时间:2013-08-06 04:04:31

标签: php random sum

我是PHP的新手,我正在尝试编写一个程序,它将生成一系列额外的总和(涉及两个从0到100随机生成两个数字)。然后,它需要让用户知道他们是否成功,并询问他们是否想要继续。我搜索了网站,但似乎没有类似的答案。提前谢谢你1

这是我到目前为止所做的事情;

  <html>
<head><title>Random addition</title></head>
<body>
<p>
<?php

$first_number = rand(1,100);
$second_number = rand(1,100);
$direct_text = "What the sum of these two number; " . "  ";
echo ($direct_text . $first_number . "". "+". "" .  $second_number) . "=". "<br />";

echo ("What do you think the answer is ?". "<br />");
?>

<?php
//If form not submitted, display form.
if (!isset($_POST['submit'])){
?>
<form method="post" action="addition.php">
  <p>Answer:
    <input type="text" name="answer">
  </p>
  <p>
    <input type="submit" name="submit" value="Submit">
  </p>
</form>

<?php
//If form submitted, process input.
}else{
//Retrieve string from form submission.
$answer = $_POST['answer'];
}
?>

<?php

$sum_total = $first_number + $second_number;


//$direct_text = "The two variables added together =  " . "<br />";
//print ($direct_text . $sum_total);

if ($answer == $sum_total) {
    echo ("Well done, Your right!" . "The answer is " . $sum_total .  "<br / >");
}
else
{
    echo ("Bad Luck.  " . " The answer is  " . $sum_total . "<br / >") ;
}
 echo "Do you want to try again?" . "<br / >" .  "y or n (y for yes and n for no)"


?>

</p>

</body>
</html>

我已经编写了一个if else语句来使用$ answer变量,但它会带来错误,我没有定义变量,但我认为我有。

1 个答案:

答案 0 :(得分:1)

你也习惯的朋友

<?php 
$a=rand(0,100); 
$b=rand(0,100); 
$c=$a+$b;
echo $a."+".$b."=".$c;  
?>