这是在PHP中正确使用if语句

时间:2015-09-24 03:31:00

标签: php html

当用户输入设定范围内的数字时,我想要显示一个字母等级的陈述。我的逻辑和语法是否正确?

<form action = student.php method="get">
   Accounting: 
   <input type=text name=accounting_grade><br><br>
   History:
   <input type=text name=history_grade><br><br>

   <input type=submit value="Click">

</form>

和php代码

<?php

    $acct_grd = $_GET['accounting_grade'];
    $hist_grd = $_GET['history_grade'];

    if($acct_grd = > 90 && < 101 ){
        echo "You earned an A in this course";
    elseif ($acct_grd = > 80 && < 91 )
        echo "You earned a B in this course";
    elseif ($acct_grd = > 70 && < 81 )
        echo "You earned a C in this course";
    elseif($acct_grd = > 60 && < 71 )
        echo "You earned an D in this course";
    elseif($acct_grd = < 60)
        echo "You earned an F in this course";

    }

?>

1 个答案:

答案 0 :(得分:1)

您的HTML代码应该更像这样。非数字值应使用引号。

<form action="student.php" method="get">
Accounting: 
<input type="text" name="accounting_grade"><br><br>
History:
<input type="text" name="history_grade"><br><br>
<input type="submit" value="submit grades">
</form>

您可以使用以下PHP代码:

<?php
$acct_grd = $_GET['accounting_grade'];

echo "for accounting...";
if($acct_grd >= 90 && $acct_grd < 101 ){
    echo "You earned an A in this course";
}elseif($acct_grd >= 80 && $acct_grd < 90 ){
    echo "You earned a B in this course";
}elseif($acct_grd >= 70 && $acct_grd < 80 ){
    echo "You earned a C in this course";
}elseif($acct_grd >= 60 && $acct_grd < 70 ){
    echo "You earned an D in this course";
}else{
echo "You earned an F in this course";
}

$acct_grd = $_GET['history_grade'];

echo " and for history...";
if($acct_grd >= 90 && $acct_grd < 101 ){
    echo "You earned an A in this course";
}elseif($acct_grd >= 80 && $acct_grd < 90 ){
    echo "You earned a B in this course";
}elseif($acct_grd >= 70 && $acct_grd < 80 ){
    echo "You earned a C in this course";
}elseif($acct_grd >= 60 && $acct_grd < 70 ){
    echo "You earned an D in this course";
}else{
echo "You earned an F in this course";
}
?>

但是,从$acct_grd = $_GET['history_grade'];开始,您可能希望将$acct_grd替换为$acct_hist,以便了解哪个变量代表哪个主题。

更改echo语句中的文本以满足您的网站需求。