计算平均值-声明变量

时间:2020-08-22 15:21:51

标签: php

现在,我正在努力如何声明平均值变量并让其计算平均值。我评论了我尝试过的一些东西。

<?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);
    //$Average = float ($one + $two + $three)/3;
    

    // 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 =  ($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>

我有某种找不到的语法错误。我当时以为是这样,但是与类中的示例代码相比,我看不到我所缺少的东西。它会跳出来吗?

我已经查看了括号,但看不到我在做错什么。我已经将其与我的类源代码进行了比较,并ing不休。我使用了验证器,它没有显示任何错误。

<?php
    // get the data from the form
    $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_INT);

    // 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
    $Average = ($one + $two + $three) / 3;}
    
?>
<!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 + $last); ?></span><br />

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

        <label>Average:</label>
        <span><?php echo number_format($Average,2); ?></span><br />
        
    </main>
</body>
</html>

0 个答案:

没有答案