【发布时间】:2015-06-16 06:16:37
【问题描述】:
这是有问题的代码行。我需要定义 $totalScore 但不确定使用哪种方法,因为它是一个结果变量。我尝试将其设置为“0”“”和“null”,但这些值并没有解决问题。这里的任何人都知道这个问题的解决方案,所以我可以将收到的成绩显示为警报?
if ($totalScore >= 900 && $totalScore <= 1000) {alert("Total Points of "+$totalScore+" gives you an A!");}
else if ($totalScore >= 800 && $totalScore <= 899) {alert("Total Points of "+$totalScore+" gives you an B!");}
else if ($totalScore >= 700 && $totalScore <= 799) {alert("Total Points of "+$totalScore+" gives you an C!");}
else if ($totalScore >= 600 && $totalScore <= 699) {alert("Total Points of "+$totalScore+" gives you an D!");}
else if ($totalScore >= 0 && $totalScore <= 599) {alert("Total Points of "+$totalScore+" gives you an F!");} }
这是其余的代码供参考!
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
if(isset($_POST['submit'])) {
$name = $_POST['StudentName'];
$quiz = (int)$_POST['QuizScore'];
$assignment = (int)$_POST['AssignmentScore'];
$midterm = (int)$_POST['MidtermScore'];
$final = (int)$_POST['FinalScore'];
$totalScore = NULL ;
$totalScore = $quiz + $assignment + $midterm + $final;
echo "Total Score: " , $totalScore; }
{
if ($totalScore >= 900 && $totalScore <= 1000) {alert("Total Points of "+$totalScore+" gives you an A!");}
else if ($totalScore >= 800 && $totalScore <= 899) {alert("Total Points of "+$totalScore+" gives you an B!");}
else if ($totalScore >= 700 && $totalScore <= 799) {alert("Total Points of "+$totalScore+" gives you an C!");}
else if ($totalScore >= 600 && $totalScore <= 699) {alert("Total Points of "+$totalScore+" gives you an D!");}
else if ($totalScore >= 0 && $totalScore <= 599) {alert("Total Points of "+$totalScore+" gives you an F!");} }
?>
<!DOCTYPE html>
<html>
<head>
<body>
<div align='center'><form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type="text" id="studentName" size="25px" placeholder="Please enter your name" name="StudentName"><br><br>
Total of Quiz Scores (150 possible):<input type="text" id="QuizScore" name="QuizScore" min="1" max="150" size="5"/><br>
Total of Assignment Scores (400 possible):<input type="text" id="AssignmentScore" name="AssignmentScore" min="1" max="400" size="5"><br>
Midterm Score (200 possible):<input type="text" id="MidtermScore" name="MidtermScore" min="1" max="200" size="5"><br>
Final Score (250 possible):<input type="text" id="FinalScore" name="FinalScore" min="1" max="250" size="5"><br>
<input type="submit" value="Calculate your score" name="submit"><br><br>
</form></div>
</body>
</html>
【问题讨论】:
-
这是因为
global这个词。只需$totalScore = null ;就可以了 -
只需分配 $totalScore = 0;或 $totalScore = null;
-
@Faradox 仍然收到未定义的变量通知
-
@ManojK 对于 $totalScore 的所有实例仍然收到相同的通知
-
为什么在 php 脚本中使用
alert()函数?