我正在尝试创建一个简单的程序来计算 3 项活动(骑自行车、慢跑和游泳)燃烧的卡路里和磅数。我不断收到错误未定义变量:calcPounds。
我在函数中有变量,但它仍然告诉我它是未定义的。我这周刚开始使用 PHP,所以我还是有点困惑。
谢谢!
PHP
function pounds_burned($calc){
$biking = 200;
$jogging = 475;
$swimming = 275;
$calories;
$pounds = 3500;
$calcPounds; // **** ASSIGNMENT MISSING HERE ****
$hoursBiking = floatval($_GET["bike"]);
$hoursJogging = floatval($_GET["jog"]);
$hoursSwimming = floatval($_GET["swim"]);
if (!is_numeric($hoursBiking) or !is_numeric($hoursJogging) or !is_numeric($hoursSwimming)) {
echo 'Enter numeric values only' . "<br />";
}
if (empty($hoursBiking) or empty($hoursJogging) or empty($hoursSwimming)) {
echo "All fields are required to be valid numbers!!";
echo "<p><a href=\"calories.html\">Try again?</a></p>\n";
}
else {
$calcPounds = round(($biking * $hoursBiking) +
($jogging * $hoursJogging) + ($swimming * $hourssSwimming));
}
}
echo "Number of pounds worked off is " . round($calcPounds);
$calories = $calcPounds / $pounds;
echo "Number of calories burned is " .round($calories);
?>
HTML
<form action="calorie_calc.php" method="GET" onsubmit="return validateForm()">
<table border="0">
<tr>
<td>Enter number of hours bicycling</td>
<td><input type="text" name="bike" id="bike" size = "10" /></td>
</tr>
<tr>
<td>Enter number of hours jogging</td>
<td><input type="text" name="jog" id="jog" size = "10" /></td>
</tr>
<tr>
<tr>
<td>Enter number of hours swimming</td>
<td><input type="text" name="swim" id="swim" size = "10" /></td>
</tr>
<td><input type="submit" name="calc" id="calc" value="Compute Pounds" /></td>
<td><input type="reset" name="resetButton" id="resetButton" value="Reset" /></td>
</tr>
</table>
</form>