为了账号安全,请及时绑定邮箱和手机立即绑定

PHP 的平均计算与变量问题

PHP 的平均计算与变量问题

PHP
森林海 2023-10-22 21:14:47
我正在尝试解决此代码的问题。我已经取得了进步,但我正在做一个简单的平均计算。当我尝试在使用变量之前声明它时,我遇到了问题。当我尝试使用它时,我收到错误消息,提示声明变量。知道我在 $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>
查看完整描述

1 回答

?
UYOU

TA贡献1878条经验 获得超4个赞

在您的 if else 代码中,您的值无法到达计算行。我编辑了这部分并包含 $one 、 $two 、 $ Three 的 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>


查看完整回答
反对 回复 2023-10-22
  • 1 回答
  • 0 关注
  • 96 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信