Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未定义变量:totalScore。由于该变量是计算的结果,我如何定义它?_Php_Html_Aptana - Fatal编程技术网

Php 未定义变量:totalScore。由于该变量是计算的结果,我如何定义它?

Php 未定义变量:totalScore。由于该变量是计算的结果,我如何定义它?,php,html,aptana,Php,Html,Aptana,下面是有问题的代码行。我需要定义$totalScore,但不确定使用哪种方法,因为它是一个结果变量。我尝试将其设置为“0”和“null”,但这些值并没有解决问题。这里有人知道这个问题的解决方案,所以我可以将收到的分数显示为警报吗 if ($totalScore >= 900 && $totalScore <= 1000) {alert("Total Points of "+$totalScore+" gives you an A!");} else

下面是有问题的代码行。我需要定义$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!");} } 

如果($totalScore>=900&&$totalScore=800&&&$totalScore=700&&$totalScore=600&&&$totalScore=0&&$totalScore请参见此。代码的改进版本

首先,您不能直接在内部使用javascript警报

<?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 = 0;;
    $totalScore = $quiz + $assignment + $midterm + $final;
     // echo "Total Score: " , $totalScore; 
     if ($totalScore >= 900 && $totalScore <= 1000) { 
        $message = "Total Points of ".$totalScore." gives you an A!"; 
    } 
    else if ($totalScore >= 800 && $totalScore <= 899) { 
        $message = "Total Points of ".$totalScore." gives you an B!" ;
   }  
    else if ($totalScore >= 700 && $totalScore <= 799) { 
        $message = "Total Points of ".$totalScore." gives you an C!"; 
    } 
   else if ($totalScore >= 600 && $totalScore <= 699) { 
        $message = "Total Points of ".'.$totalScore.'." gives you an D!"; 
   } 
    else if ($totalScore >= 0 && $totalScore <= 599) { 
        $message = "Total Points of ".$totalScore." gives you an F!";
    } 
   echo '<script type="text/javascript">  alert("'.$message.'");        </script>'; 
  }
   ?>

<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>

我做了一些更改,请查看下面的代码,实际上您必须在发布条件之前声明$totalscore,并且整个代码都应该处于条件中


变量
$totalScore
将不会初始化,当此条件
如果(isset($\u POST['submit'])
为false时,因为有太多的
{
}
在您的代码中。因此,我会检查为什么未设置post变量
submit
,更重要的是,在另一种情况下也要初始化变量。

您正在初始化
$totalScore
仅当表单已提交时,首先应该从一开始就用
null
初始化它

<?php
  $totalScore = null;

  if (isset($_POST['submit']) {
     //...
  } elseif ($totalScore && (/*your initial conditions*/)) {
     //calculate marks
  }

这是因为
global
这个词。只需
$totalScore=null;
就可以了,只需分配$totalScore=0;或$totalScore=null;@Faradox仍在接收未定义的变量notices@ManojK对于$totalScore的所有实例,仍然会收到相同的通知为什么要使用
alert()
php脚本中的函数?此代码没有未定义的变量:totalScore warning/error就是这样!谢谢!如何使用php中的+这是(加法运算符)在php内部发出警报。您的代码将给出未定义函数警报()的错误@kevin代码现在正在运行,只是它的else if语句部分得到了这个错误总分:813致命错误:调用未定义函数alert()在/home/jamcgraw/public_html/index1.php中联机18@user9018你能运行吗?我发布的答案中的代码。对于警报用户Meenesh Jain的解决方案,最好是将消息存储在一个变量中,然后在所有条件之后,只需使用它标记进行回显。
<?php
  $totalScore = null;

  if (isset($_POST['submit']) {
     //...
  } elseif ($totalScore && (/*your initial conditions*/)) {
     //calculate marks
  }