PHP 的平均计算与变量问题

我正在尝试解决此代码的问题。我已经取得了进步,但我正在做一个简单的平均计算。当我尝试在使用变量之前声明它时,我遇到了问题。当我尝试使用它时,我收到错误消息,提示声明变量。


知道我在 $Average 部分做错了什么吗?我要么无法渲染页面,要么得到 0.00,因为当我声明变量时,我还没有输入。


<?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');


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



森林海
浏览 97回答 1
1回答

UYOU

在您的 if else 代码中,您的值无法到达计算行。我编辑了这部分并包含 $one 、 $two 、 $ Three 的 isset :<?php&nbsp; &nbsp; // get the data from the form&nbsp; &nbsp; $first = filter_input(INPUT_POST, 'first');&nbsp; &nbsp; $last = filter_input(INPUT_POST, 'last');&nbsp; &nbsp; $one = filter_input(INPUT_POST, 'one',&nbsp; &nbsp; &nbsp; &nbsp; FILTER_VALIDATE_FLOAT);&nbsp; &nbsp; $two = filter_input(INPUT_POST, 'two',&nbsp; &nbsp; &nbsp; &nbsp; FILTER_VALIDATE_FLOAT);&nbsp; &nbsp; $three = filter_input(INPUT_POST, 'three',&nbsp; &nbsp; &nbsp; &nbsp; FILTER_VALIDATE_FLOAT);&nbsp; &nbsp; //var = $Average ;&nbsp; &nbsp; $Sum = filter_input(INPUT_POST, 'Sum');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!isset($one)){$one=0;}else{$error_message ='You must enter a value';}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!isset($two)){$two=0;}else{$error_message ='You must enter a value';}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!isset($three)){$three=0;}else{$error_message ='You must enter a value';}&nbsp; &nbsp; // validate Score one&nbsp; &nbsp; if ($one === FALSE ) {&nbsp; &nbsp; &nbsp; &nbsp; $error_message = 'Score one must be a valid number.';&nbsp;&nbsp; &nbsp; } else if ( $one < 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; $error_message = 'Score one cannot be less than zero.';&nbsp;&nbsp; &nbsp;// validate Score two}&nbsp; &nbsp;}&nbsp; &nbsp; if ($two === FALSE ) {&nbsp; &nbsp; &nbsp; &nbsp; $error_message = 'Score two must be a valid number.';&nbsp;&nbsp; &nbsp; } else if ( $two < 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; $error_message = 'Score two cannot be less than zero.';&nbsp;&nbsp; &nbsp; // validate Score three&nbsp; &nbsp; }&nbsp; &nbsp; if ($three === FALSE ) {&nbsp; &nbsp; &nbsp; &nbsp; $error_message = 'Score three must be a valid number.';&nbsp;&nbsp; &nbsp; } else if ( $three < 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; $error_message = 'Score three cannot be less than zero.';&nbsp;&nbsp; &nbsp; // set error message to empty string if no invalid entries&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $error_message = ''; }&nbsp; &nbsp; // if an error message exists, go to the index page&nbsp; &nbsp; if ($error_message != '') {&nbsp; &nbsp; &nbsp; &nbsp; include('index.php');&nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // calculate the average score&nbsp; &nbsp; &nbsp; &nbsp; //$Sum = $one; + $two; + $three;&nbsp; &nbsp; &nbsp; &nbsp; //$Average = $Sum / 3;}}&nbsp; &nbsp; &nbsp; &nbsp; $Average = ($one + $two + $three)/3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $Average = number_format($Average, 2);&nbsp; &nbsp;&nbsp;?><!DOCTYPE html><html><head>&nbsp; &nbsp; <title>Assignment 2</title>&nbsp; &nbsp; <link rel="stylesheet" type="text/css" href="main.css"/></head><body>&nbsp; &nbsp; <main>&nbsp; &nbsp; &nbsp; &nbsp; <h1>Assignment 2</h1>&nbsp; &nbsp; &nbsp; &nbsp; <label>Student Name:</label>&nbsp; &nbsp; &nbsp; &nbsp; <span><?php echo $first; ?></span><span><?php echo " ",&nbsp; $last; ?></span><br />&nbsp; &nbsp; &nbsp; &nbsp; <label>Your Scores:</label>&nbsp; &nbsp; &nbsp; &nbsp; <span><?php echo $one, ","; ?></span> <span><?php echo $two, ","; ?></span> <span><?php echo $three; ?></span><br />&nbsp; &nbsp; &nbsp; &nbsp; <label>Average:</label>&nbsp; &nbsp; &nbsp; &nbsp; <span><?php echo $Average; ?></span><br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </main></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP