我正在尝试创建一个简单的程序,计算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>
答案 0 :(得分:2)
在您的档案calorie_calc.php
<?php
if (isset($_GET["calc"])){
$biking = 200;
$jogging = 475;
$swimming = 275;
$calories = 0;
$pounds = 3500;
$calcPounds = 0;
$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);
}
?>
答案 1 :(得分:0)
您的变量$calcpounds
确实位于函数内部,但这不是它的使用位置。您的代码将始终执行以下行:
echo "Number of pounds worked off is " . round($calcPounds);
$calories = $calcPounds / $pounds;
echo "Number of calories burned is " .round($calories);
在函数之外,因此$calcpounds
未初始化。
$calcpounds
也不保证在你的函数内初始化,但即使它是,它也不会在外面可见。
您需要return
该函数中的值才能在以后使用该值,但在此您甚至不会调用pounds_burned()
让它返回值。
我建议您重新构建代码:
答案 2 :(得分:0)
如果您处于生产模式,最好不要显示E_ALL
之类的错误,这是Notice
错误,当然如果您正试图摆脱此问题
if (empty($hoursBiking) or empty($hoursJogging) or empty($hoursSwimming)) {
$calcPounds = null; // Add this line for a quick fix, otherwise refactor whole code
echo "All fields are required to be valid numbers!!";
echo "<p><a href=\"calories.html\">Try again?</a></p>\n";
}