非常简单的PHP测验实践(IF ELSEIF ELSE条件)不能正常工作

时间:2015-01-13 05:10:21

标签: php if-statement

我正在尝试教我自己的PHP和练习我决定创建一个简单的测验。单选按钮选项的值为整数0到2,具体取决于问题。

PHP脚本只是从表单中提取值,添加它们并将数据存储在 $ sum 中 消息应出现在三个可能的条件下。最后一个条件没有正确呈现。 以下link表示存储 $ sum 的值

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $one = ($_POST["one"]);
  $two = ($_POST["two"]);
  $three = ($_POST["three"]);
  $four = ($_POST["four"]);
  $five = ($_POST["five"]);

    $sum = $one + $two + $three + $four + $five;


if ($sum <= 2){
$answer = "Sorry you are not cut out to run your own business!!";
}
elseif ($sum > 2 && $sum <= 4){
$answer = "You are a hustler by nature";
}
elseif ($sum > 4 && $sum <= 7){
$answer = "You are a tycoonist, that will take over the world";
}
else { 
$answer = "";
}

2 个答案:

答案 0 :(得分:3)

你错过了{in the line:

elseif ($sum > 2 && $sum <= 4)

答案 1 :(得分:1)

您未在elseif ($sum > 2 && $sum <= 4)之后关闭括号。使用以下代码

 if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $one = ($_POST["one"]);
  $two = ($_POST["two"]);
  $three = ($_POST["three"]);
  $four = ($_POST["four"]);
  $five = ($_POST["five"]);

    $sum = $one + $two + $three + $four + $five;


if ($sum <= 2){
$answer = "Sorry you are not cut out to run your own business!!";
}
elseif ($sum > 2 && $sum <= 4){
$answer = "You are a hustler by nature";
}
elseif ($sum > 4 && $sum <= 7){
$answer = "You are a tycoonist, that will take over the world";
}
else { 
$answer = "";
}

希望这有助于你