0

我正在尝试创建一个简单的程序来计算 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>
4

3 回答 3

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);
}
?>
于 2013-09-01T00:49:51.520 回答
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()让它返回一个值。

我建议你重构你的代码:

  • 从函数中删除验证:将其作为主程序流程的一部分执行
  • 将三个变量($hoursJogging 等)作为参数传递给函数。
  • 让函数只执行算术,并返回结果
  • 存储结果并根据需要输出消息。
于 2013-09-01T00:18:38.563 回答
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";
}
于 2013-09-01T00:19:07.570 回答