三个数字的平均值

时间:2016-10-25 04:44:41

标签: php

我有一个Form.html和Form.php文件。我的Form.html文件有3个文本输入框,要求用户在每个输入框中输入一个数字。我希望在用户点击提交后回显平均数。这是我到目前为止,虽然我点击提交后平均数是“0”

// The Average of Numbers
$number1 = ['number3'];
$number2 = ['number3'];
$number3 = ['number3'];

// How Many Numbers are in Our Set
$numbersInSet = 3;

// Get the Sum of the Numbers
$average = $sum / $numbersInSet;

echo "The average of the three numbers you entered is<b> $average<p>";

1 个答案:

答案 0 :(得分:0)

<?php
// The Average of Numbers
$number1 = isset($_POST['number1']);
$number2 = isset($_POST['number2']);
$number3 = isset($_POST['number3']);

// How Many Numbers are in Our Set
$numbersInSet = 3;

if(is_numeric($number1) &&is_numeric($number2)&&is_numeric($number3)){
$sum=$number1+$number2+$number3;

// Get the Sum of the Numbers
$average = $sum / $numbersInSet;
echo "The average of the three numbers you entered is<b> $average<p>";
}

?>

<form method="post">
<input type="text" name="number1" />
<input type="text" name="number2" />
<input type="text" name="number3" />
<input type="submit">
</form>