Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.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的平均计算_Php - Fatal编程技术网

具有变量问题的PHP的平均计算

具有变量问题的PHP的平均计算,php,Php,大家早上好 我正在尝试对此代码进行故障排除。我取得了进步,但我正在做一个简单的平均计算。当我尝试在使用变量之前声明它时,我遇到了问题。当我试图直接使用它时,会出现错误,比如声明一个变量 你知道我在做什么吗?平均部分是美元。我要么无法呈现页面,要么得到0.00,因为当我声明变量时,我还没有它的输入 任务2 任务2 学生姓名: 你的分数: 平均值: 在if-else代码中,您的值无法到达计算行。 我编辑了这一部分,包括一美元、二美元和三美元的isset: <?php //

大家早上好

我正在尝试对此代码进行故障排除。我取得了进步,但我正在做一个简单的平均计算。当我尝试在使用变量之前声明它时,我遇到了问题。当我试图直接使用它时,会出现错误,比如声明一个变量

你知道我在做什么吗?平均部分是美元。我要么无法呈现页面,要么得到0.00,因为当我声明变量时,我还没有它的输入


任务2
任务2
学生姓名:

你的分数:
平均值:

在if-else代码中,您的值无法到达计算行。 我编辑了这一部分,包括一美元、二美元和三美元的isset:

<?php

    // get the data from the form
    $first = filter_input(INPUT_POST, 'first');
    $last = filter_input(INPUT_POST, 'last');
    $one = filter_input(INPUT_POST, 'one',
        FILTER_VALIDATE_FLOAT);
    $two = filter_input(INPUT_POST, 'two',
        FILTER_VALIDATE_FLOAT);
    $three = filter_input(INPUT_POST, 'three',
        FILTER_VALIDATE_FLOAT);
    //var = $Average ;
    $Sum = filter_input(INPUT_POST, 'Sum');
    
            if(!isset($one)){$one=0;}else{$error_message ='You must enter a value';}
            if(!isset($two)){$two=0;}else{$error_message ='You must enter a value';}
            if(!isset($three)){$three=0;}else{$error_message ='You must enter a value';}

    // validate Score one
    if ($one === FALSE ) {
        $error_message = 'Score one must be a valid number.'; 
    } else if ( $one < 0 ) {
        $error_message = 'Score one cannot be less than zero.'; 
   // validate Score two}
   }
    if ($two === FALSE ) {
        $error_message = 'Score two must be a valid number.'; 
    } else if ( $two < 0 ) {
        $error_message = 'Score two cannot be less than zero.'; 
    // validate Score three
    }
    if ($three === FALSE ) {
        $error_message = 'Score three must be a valid number.'; 
    } else if ( $three < 0 ) {
        $error_message = 'Score three cannot be less than zero.'; 
    // set error message to empty string if no invalid entries
    } else {
        $error_message = ''; }

    // if an error message exists, go to the index page
    if ($error_message != '') {
        include('index.php');
        exit();
    }
            
    
    // calculate the average score
        //$Sum = $one; + $two; + $three;
        //$Average = $Sum / 3;}}
        $Average = ($one + $two + $three)/3;
            
        $Average = number_format($Average, 2);
    
?>
<!DOCTYPE html>
<html>
<head>
    <title>Assignment 2</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
    <main>
        <h1>Assignment 2</h1>

        <label>Student Name:</label>
        <span><?php echo $first; ?></span><span><?php echo " ",  $last; ?></span><br />

        <label>Your Scores:</label>
        <span><?php echo $one, ","; ?></span> <span><?php echo $two, ","; ?></span> <span><?php echo $three; ?></span><br />

        <label>Average:</label>
        <span><?php echo $Average; ?></span><br />
        
    </main>
</body>
</html>

任务2
任务2
学生姓名:

你的分数:
平均值:

一些合理的代码缩进将是一个好主意。它帮助我们阅读代码,更重要的是,它将帮助您调试自己的代码。为了你自己的利益,快速看一看a。嘿!你的代码看起来很长。如果你能把它删减到只有引起错误的代码,这将有助于回答者。谢谢你的帮助。所以我把重点放在了我的平均计算上,而我处理错误的方式实际上阻止了它的计算。再次感谢您提供了比建议我调整间距并解决它更有用的内容。:-)我正在努力学习,你的回答真的帮助了我。